2010-11-02 82 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颇为混乱。如果它是一个函数,请使用小写字母,如果您将它用作构造函数,则使用类名称的大写字母。对我来说,这个函数必须是一个构造函数。

相关问题