按顺序下载文件数组Adobe flex

时间:2012-11-12 16:32:32

标签: arrays flex download filestream

此代码由此处的某人编写。我试图将它用于一个项目,但它似乎一次下载所有文件。是否可以按顺序下载阵列中的文件?这就完成了下载fr数组[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);
        }
    }
]]>

1 个答案:

答案 0 :(得分: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