0

这段代码是这里有人写的。我正在尝试将它用于一个项目,但它似乎是一次下载所有文件。是否可以按顺序下载数组中的文件?即完成下载 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);
        }
    }
]]>

4

1 回答 1

0
private var arrayOfFiles:Array = ["file1.xml", "file2.xml"];
private var currentFile:int = 0;

private function downloadNextFile():void{
    finishWriteFile();
    if(currentFile < arrayOfFiles.lenght){
        var remoteURL = "http://www.domain.com/" + arrayOfFiles[currentFile];
        var localURL = "C:/dir/" + arrayOfFiles[currentFile];
        currentFile++;
        downloadFile(remoteURL, localURL);

        ... your other code
    } else {
        // all files are downloaded
    }
}

然后像这样更改完整的侦听器:

    urlStream.addEventListener(Event.COMPLETE, downloadNextFile);

这将等到文件 0 完成后调用 downloadNextFile ,这将是 1... 等等。

编辑:

添加在对 finishWriteFile() 的调用中,因为它也需要调用 onComplete

于 2012-11-12T17:28:23.890 回答