Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex URLStream在我的flex AIR应用程序中抛出错误#2029_Apache Flex_Actionscript 3_Air - Fatal编程技术网

Apache flex URLStream在我的flex AIR应用程序中抛出错误#2029

Apache flex URLStream在我的flex AIR应用程序中抛出错误#2029,apache-flex,actionscript-3,air,Apache Flex,Actionscript 3,Air,在我的AIR应用程序中,我尝试使用URLStream实现一个文件下载程序 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

在我的AIR应用程序中,我尝试使用URLStream实现一个文件下载程序

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); } } } 公共类文件下载程序{ //类从internet下载文件 //每次数据到达时调用的函数 //调用时带有已下载多少的参数 public var onProgress:Function=Function(loaded:Number,total:Number):void{}; 公共变量onComplete:Function=Function():void{}; public-var-remotePath:String=“”; public var localFile:File=null; 正在运行的公共变量:Boolean=false; 公共变量流:URLStream; 私有var fileAccess:FileStream; 公共函数FileDownloader(remotePath:String=”“,localFile:File=null){ this.remotePath=远程路径; this.localFile=localFile; } 公共函数加载():void { 尝试 { 流=空; 如果(!stream | |!stream.connected) { stream=新的URLStream(); fileAccess=newfilestream(); var请求者:URLRequest=新的URLRequest(远程路径); var currentPosition:uint=0; var downloadCompleteFlag:Boolean=false; //函数调用oncomplete,一旦下载完成 //所有数据都已写入光盘 addEventListener(“outputProgress”,函数(结果):void{ 如果(result.bytesPending==0&&downloadCompleteFlag){ stream.close(); fileAccess.close(); 运行=错误; onComplete(); } }); openAsync(localFile,FileMode.WRITE); fileAccess.addEventListener(IOErrorEvent.IO_错误,函数(e:IOErrorEvent) { 跟踪(“远程路径:”+remotePath); 跟踪('wrintg….'时发生io错误+e.toString()); }); stream.addEventListener(IOErrorEvent.IO_ERROR,函数(e:IOErrorEvent) { 跟踪(“远程路径:”+remotePath); 跟踪('流有IO错误:'+e.text); }); stream.addEventListener(“进度”),函数(e:ProgressEvent):void{ 变量字节:ByteArray=newbytearray(); var thisStart:uint=当前位置; currentPosition+=stream.bytes可用; //^^确保异步不会破坏任何东西 尝试 { //跟踪('从'+remotePath+'…'读取]); readBytes(字节,thisStart); fileAccess.writeBytes(字节,thisStart); } 捕获(错误:错误) { 跟踪(“远程路径:”+remotePath); trace(“+err.name+':'+err.message写入字节时出错); if(stream.connected) stream.close(); 中止(); onComplete(); 返回; } onProgress(例如bytesLoaded,例如ByTestTotal); }); addEventListener(“完成”,函数():void{ downloadCompleteFlag=true; }); stream.load(请求者); }否则{ //做一些说不出的事 } 运行=真; } 捕获(错误:错误) { trace('下载文件时出错:'+err); } } 公共函数中止():无效{ 试一试{ stream.close(); 跟踪(“流关闭”); 运行=错误; } 捕获(错误:错误){ 跟踪(“中止下载时出错”); 跟踪(err); } } } 我只需创建上述类的一个对象,并传递url和文件,然后调用load函数。对于某些文件,我得到以下错误

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. 远程路径:http://mydomain.com/238/6m_608-450.jpg 从写入字节时出错…错误:错误#2029:此URLStream对象未打开流。
这意味着错误来自我正在使用的文件流(fileAccess)。我无法理解为什么会发生这种情况。如果我试图打开url
http://mydomain.com/238/6m_608-450.jpg
在浏览器中,它可以正常打开。对于某些文件,这是随机发生的。有什么问题吗?

我在办公室试过,它对我很有效(适用于不同的文件和文件大小)。 那么,你能描述一下不适合你的文件(或类型文件)吗(如果可以的话,可以发布一个url)? 我想说的是,当您使用readBytes方法时,您的流(因此URLStream)总是关闭的

此外,我给我一些建议: 1/使用flash的常量而不是简单的字符串 2/操作完成后,不要忘记删除侦听器 3/您的方法FileDownloader非常混乱。如果是函数,则使用小写;如果将类用作构造函数,则在类的名称中加上大写字母。对我来说,这个函数必须是一个构造函数