在我的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
,它會正常打開。這對某些文件是隨機發生的。可能是什麼問題呢?