2012-11-12 62 views
0

此代碼由某人在此處編寫。我正在嘗試將它用於一個項目,但它似乎一次下載所有文件。是否可以按順序下載數組中的文件?這是完成下載fr數組[0],然後移動到下一個文件。這可能嗎?請幫忙。按順序下載文件陣列Adobe flex

<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); 
     } 
    } 
]]> 

+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 whhich爲1 ...等等

編輯:

添加到finishWriteFile()調用中,因爲它需要調用onComplete也