Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:28:57+00:00 2026-05-27T16:28:57+00:00

i found a few scripts online and combined them to this. I want to

  • 0

i found a few scripts online and combined them to this.
I want to download files from the web to my local harddrive.
Any idea what i’m doing wrong?

var fs:FileStream;
var stream:URLStream;
var _output:Boolean = false;

init();
startDownload('http://www.teachenglishinasia.net/files/u2/purple_lotus_flower.jpg');

function init() { 
    stream = new URLStream();
    stream.addEventListener(ProgressEvent.PROGRESS, _dlProgressHandler); 
    stream.addEventListener(Event.COMPLETE, _dlCompleteHandler);
    stream.addEventListener(Event.OPEN, _dlStartHandler);
    fs = new FileStream();
    fs.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, _writeProgressHandler)
}

function startDownload(url:String):void {
     //fs.openAsync(lfile, FileMode.APPEND);
     _output = false;
     stream.load(new URLRequest(url));
}

function downloadComplete():void {
     var fileData:ByteArray = new ByteArray();
     stream.readBytes(fileData,0,stream.bytesAvailable);
     fs.writeBytes(fileData,0,fileData.length);
     fs.close(); 
}

function writeToDisk():void {
     _output = false;
     var fileData:ByteArray = new ByteArray();
     stream.readBytes(fileData,0,stream.bytesAvailable);
     fs.writeBytes(fileData,0,fileData.length);
}

function _dlProgressHandler(evt:ProgressEvent):void {
     if(_output){
         writeToDisk();   
     }
}

function _dlCompleteHandler(evt:Event):void { 
    downloadComplete();
} 

function _dlStartHandler(evt:Event):void {
     _output = true; 
}

function _writeProgressHandler(evt:OutputProgressEvent):void{
     _output = true;
}

Flash keeps telling me: Error: Error #2029: This URLStream object does not have a stream opened. However the connection to the webpage goes out.

Any ideas?
Thank you for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:28:58+00:00Added an answer on May 27, 2026 at 4:28 pm

    I modified your code and here is a working Downloader class. (@SébastienNussbaumer improved this answer and @TobiasKienzler reviewed changes: thanks a lot guys!)

    Simple to use:

    var downLoader:Downloader = new Downloader();
    downLoader.addEventListener(DownloadEvent.DOWNLOAD_COMPLETE, function(event:DownloadEvent):void{
        trace("Download complete: ");
        trace("\t"+event.url);
        trace("->\t"+event.file.url);
    });
    var file:File = File.applicationStorageDirectory.resolvePath("downloaded.mp3");
    downLoader.download("http://dl.dropbox.com/u/18041784/Music/Lana%20Del%20Rey%20-%20Born%20To%20die%20%28Gemini%20Remix%29.mp3", file);
    

    Output when download complete:

    Download complete: 
        [audio src="http://dl.dropbox.com/u/18041784/Music/Lana%20Del%20Rey%20-%20Born%20To%20die%20%28Gemini%20Remix%29.mp3" /]
    ->  app-storage:/downloaded.mp3
    

    Enjoy:-)

    package com.tatstyappz.net
    {
        import flash.events.DataEvent;
        import flash.events.Event;
        import flash.events.EventDispatcher;
        import flash.events.OutputProgressEvent;
        import flash.events.ProgressEvent;
        import flash.filesystem.File;
        import flash.filesystem.FileMode;
        import flash.filesystem.FileStream;
        import flash.net.URLRequest;
        import flash.net.URLStream;
        import flash.utils.ByteArray;
    
        public class Downloader extends EventDispatcher
        {
            [Event(name="DownloadComplete", type="com.tatstyappz.net.DownloadEvent")]
    
            private var file:File;
            private var fileStream:FileStream;
            private var url:String;
            private var urlStream:URLStream;
    
            private var waitingForDataToWrite:Boolean = false;
    
            public function Downloader()
            {
                urlStream = new URLStream();
    
                urlStream.addEventListener(Event.OPEN, onOpenEvent);
                urlStream.addEventListener(ProgressEvent.PROGRESS, onProgressEvent); 
                urlStream.addEventListener(Event.COMPLETE, onCompleteEvent);
    
                fileStream = new FileStream();
                fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, writeProgressHandler)
    
            }
    
            public function download(formUrl:String, toFile:File):void {
                this.url = formUrl;
                this.file = toFile;
                fileStream.openAsync(file, FileMode.WRITE);
                urlStream.load(new URLRequest(url));
            }
    
            private function onOpenEvent(event:Event):void {
                waitingForDataToWrite = true;
    
                dispatchEvent(event.clone());
            }
    
            private function onProgressEvent(event:ProgressEvent):void {
                if(waitingForDataToWrite){
                    writeToDisk();
                    dispatchEvent(event.clone());
                }
            }
    
            private function writeToDisk():void {
                var fileData:ByteArray = new ByteArray();
                urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
                fileStream.writeBytes(fileData,0,fileData.length);
                waitingForDataToWrite = false;
    
                dispatchEvent(new DataEvent(DataEvent.DATA));
            }
    
            private function writeProgressHandler(evt:OutputProgressEvent):void{
                waitingForDataToWrite = true;
            }
    
            private function onCompleteEvent(event:Event):void {
                if(urlStream.bytesAvailable>0)
                    writeToDisk();
                fileStream.close();
    
                fileStream.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, writeProgressHandler);
    
                dispatchEvent(event.clone());
                // dispatch additional DownloadEvent
                dispatchEvent(new DownloadEvent(DownloadEvent.DOWNLOAD_COMPLETE, url, file));
            }
    
        }
    }
    

    And the event class:

    package com.tatstyappz.net
    {
        import flash.events.Event;
        import flash.filesystem.File;
    
        public class DownloadEvent extends Event
        {
            public static const DOWNLOAD_COMPLETE:String = "DownloadComplete";
    
            public var url:String;
            public var file:File;
    
            public function DownloadEvent(type:String, url:String, file:File)
            {
                super(type, true);
                this.url = url;
                this.file = file;
            }
    
            override public function toString():String{
                return super.toString() + ": "+ url + " -> "+file.url;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a few questions similar to this one here on SO, but none
I've found a few other people asking this question, but the answers for their
I've found a few answers for this using mySQL alone, but I was hoping
I've searched around SO for this and found a few things, but I'm still
I'm writing a few little bash scripts under Ubuntu linux. I want to be
I've recorded a few scripts with this tool, but when I run it, 100%
I have question regarding disabling browser caching. I have already found few solutions, and
I've found a few tutorials that explain how to use the windows API to
I have found a few samples on how to use GameKit for bluetooth communication
I've found a few answers on how to change your company name, but is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.