2010-11-02 55 views
0

在我的AIR應用程序中,我嘗試使用URLStream實現文件下載器。URLStream在我的flex AIR應用程序中拋出錯誤#2029

 
public class FileDownloader {  
     // Class to download files from the internet 

     // Function called every time data arrives 
     // called with an argument of how much has been downloaded 
     public var onProgress :Function = function(loaded:Number, total:Number):void{}; 
     public var onComplete :Function = function():void{}; 
     public var remotePath :String = ""; 
     public var localFile :File = null; 
     public var running:Boolean = false; 

     public var stream :URLStream; 
     private var fileAccess :FileStream; 

     public function FileDownloader(remotePath :String = "" , localFile :File = null) {    
      this.remotePath = remotePath; 
      this.localFile = localFile; 
     } 

     public function load() :void 
     { 
      try 
      { 
       stream = null; 
       if(!stream || !stream.connected) 
       { 
        stream = new URLStream(); 
        fileAccess = new FileStream(); 

        var requester :URLRequest = new URLRequest(remotePath); 
        var currentPosition :uint = 0; 
        var downloadCompleteFlag :Boolean = false; 

        // Function to call oncomplete, once the download finishes and 
        // all data has been written to disc        
        fileAccess.addEventListener("outputProgress", function (result):void { 
         if(result.bytesPending == 0 && downloadCompleteFlag) {       
          stream.close(); 
          fileAccess.close(); 
          running = false; 
          onComplete(); 
         } 
        }); 


        fileAccess.openAsync(localFile, FileMode.WRITE); 

        fileAccess.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent) 
        { 
         trace('remotePath: '+remotePath); 
         trace('io error while wrintg ....'+e.toString()); 
        }); 

        stream.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent) 
        { 
         trace('remotePath: '+remotePath); 
         trace('There was an IO error with the stream: '+e.text); 
        }); 

        stream.addEventListener("progress" , function (e:ProgressEvent) :void { 
         var bytes :ByteArray = new ByteArray(); 
         var thisStart :uint = currentPosition; 
         currentPosition += stream.bytesAvailable; 
         // ^^ Makes sure that asyncronicity does not break anything 

         try 
         { 
          //trace('reading from '+remotePath+' ...'); 
          stream.readBytes(bytes, thisStart); 
          fileAccess.writeBytes(bytes, thisStart); 
         } 
         catch(err:Error) 
         { 
          trace('remotePath: '+remotePath); 
          trace('error while writing bytes from...'+err.name+':'+err.message); 
          if(stream.connected) 
           stream.close(); 

          abort(); 
          onComplete(); 
          return; 
         } 

         onProgress(e.bytesLoaded, e.bytesTotal);           
        }); 

        stream.addEventListener("complete", function() :void { 
         downloadCompleteFlag = true; 
        }); 

        stream.load(requester); 

       } else { 
        // Do something unspeakable 
       } 
       running = true; 
      } 
      catch(err:Error) 
      { 
       trace('error while downloading the file: '+err); 
      } 
     } 

     public function abort():void { 
      try { 
       stream.close(); 
       trace('stream closed'); 
       running = false; 
      } 
      catch(err:Error) { 
       trace('error while aborting download'); 
       trace(err); 
      } 
     } 
    } 

我只是創建一個上述類的對象,並傳遞url和文件並調用加載函數。對於某些文件,我收到以下錯誤。

remotePath: http://mydomain.com/238/6m_608-450.jpg 
error while writing bytes from...Error:Error #2029: This URLStream object does not have a stream opened.

這意味着錯誤來自我正在使用的文件流(fileAccess)。我無法弄清楚爲什麼會發生這種情況。如果我嘗試在瀏覽器中打開網址http://mydomain.com/238/6m_608-450.jpg,它會正常打開。這對某些文件是隨機發生的。可能是什麼問題呢?

回答

1

我在我的辦公室嘗試過,它適用於我(用於不同文件和文件大小)。 那麼,你能描述一下不適合你的文件(或者類型文件)嗎(如果可以的話發佈一個url)? 我會說,當你使用方法readBytes你的流(所以URLStream)是永遠接近。

更多,我可以讓我的一些建議:而不是簡單的字符串的 1 /使用Flash的常量 2 /不要忘記刪除你的聽衆一旦操作完成 3 /你的方法FileDownloader頗爲混亂。如果它是一個函數,請使用小寫字母,如果您將它用作構造函數,則使用類名稱的大寫字母。對我來說,這個函數必須是一個構造函數。

相關問題