这段代码是这里有人写的。我正在尝试将它用于一个项目,但它似乎是一次下载所有文件。是否可以按顺序下载数组中的文件?即完成下载 fr array[0] 然后继续下一个文件。这可能吗?请帮忙。
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="200"
title=" Updater"
showStatusBar="false">
<fx:Style source="Main.css"/>
<fx:Script>
<![CDATA[
private function download_clickHandler(event:MouseEvent):void
{
var filearray:Array = new Array();
filearray[0]="pregame.jpg";
filearray[1]="01.jpg";
filearray[2]="02.jpg";
filearray[3]="03.jpg";
filearray[4]="04.jpg";
filearray[5]="05.jpg";
filearray[6]="06.jpg";
filearray[7]="07.jpg";
filearray[8]="08.jpg";
filearray[9]="09.jpg";
filearray[10]="10.jpg";
for (var i:uint; i < filearray.length; i++) {
var remoteURL = "http://www.domain.com/" + filearray[i];
var localURL = "C:/dir/" + filearray[i];
downloadFile(remoteURL, localURL);
}
function downloadFile(url, fileName) {
// Create the stream for the data request
var urlStream = new URLStream();
// Used to initiate request for remote file
var request = new URLRequest(url);
// Create file stream
var fileStream = new FileStream();
// Create a reference to a location on disk
var file = File.desktopDirectory.resolvePath(fileName);
// Called as download progresses
var writeFile = function()
{
// Write to file
if (urlStream.bytesAvailable > 51200)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
return true;
}
// Called when download completes
var finishWriteFile = function()
{
// Write to file
if(urlStream.bytesAvailable > 0)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
// Close streams
fileStream.close();
urlStream.close();
return true;
}
// Initiate download
fileStream.openAsync(file, FileMode.WRITE);
urlStream.load(request);
// Add event listeners
urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
}
}
]]>