Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3 multiple file download, check when completed?
    text
    copied!<p>I am looping through a small array of file names to grab from a remote server and save locally. This bit works OK.</p> <p>The problem is, I need to run my next function when the downloads are complete.</p> <p>Currently, my function, lets call it step2(), gets called before all the FileStreams can finish (infact, its called before that function even starts!)</p> <p>I read that there is a complete event listener when using openAsynch, but I need to check that all files, e.g. 3 remote swfs) have been written and then start step2() function.</p> <p>How can I go about this?</p> <p>Code below:</p> <pre><code>function onPlaylistComplete(e:Event) { var myArray:Array = new Array(); for each(var i in list_of_files) { myArray.push(i); } for(i = 0; i &lt; myArray.length; i++) { swfItem = myArray[i]; var urlString:String = site_url + myArray[i]; var urlReq:URLRequest = new URLRequest(urlString); var urlStream:URLStream = new URLStream(); urlStream.addEventListener(Event.COMPLETE, urlStreamComplete(swfItem)); urlStream.load(urlReq); } function urlStreamComplete(swfItem):Function { var downloadSwf:Function = function (event:Event):void { var fileData:ByteArray = new ByteArray(); var stream = event.target; stream.readBytes(fileData, 0, stream.bytesAvailable); try { var f : File = File.documentsDirectory.resolvePath(swfItem); var fs : FileStream = new FileStream(); fs.addEventListener(Event.COMPLETE, fileCompleteHandler); fs.openAsync(f, FileMode.WRITE); fs.writeBytes(fileData); } catch (err : Error) { trace(err.message); } } return downloadSwf; } function fileCompleteHandler(e:Event):void { e.target.close(); } step2(); // this seems to run before UrlStreamComplete() } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload