【发布时间】:2016-04-12 17:42:15
【问题描述】:
我有一个应用程序正在下载几个大型二进制文件并将它们保存到磁盘。在某些机器上它可以正常工作,而在其他一些机器上,每隔一段时间,下载将进行到 99.9% 完成,并且 URLStream 对象不会触发 Event.COMPLETE
这与此处出现的问题几乎相同:
Why does URLStream complete event get dispatched when the file is not finished loading?
我已尝试使用其中一个答案中描述的“Cache Bust”方法,但仍然没有骰子。
任何帮助将不胜感激。
这里有一些示例代码来帮助说明我正在尝试做的事情:
var contentURL:String = "http://some-large-binary-file-in-amazon-s3.tar";
var stream:URLStream = new URLStream();
stream.addEventListener(Event.COMPLETE, function(e:Event):void{
//This should fire when the file is done downloading
//On some systems this fails to fire once in a while
//On other systems it never fails to fire
});
stream.addEventListener(ProgressEvent.PROGRESS, function(pe:ProgressEvent):void{
//Write the bytes available in the stream and save them to disk
//Note that a download will reach 100% complete in terms of total progress but the 'complete' event might still not fire.
});
var urlRequest:URLRequest = new URLRequest(contentURL);
//Here we might add some headers to the URL Request for resuming a file
//but they don't matter, the 'Event.COMPLETE' will fail to fire with our without
//these headers
addCustomHeaders( urlRequest );
stream.load( urlRequest );
【问题讨论】:
-
我已更新帖子以包含代码示例。
-
*“在某些系统上,它会偶尔触发一次......在其他系统上它永远不会失败......”我会首先检查那些失败的系统。这是一个互联网连接问题(即:他们有什么东西会切断大量下载吗?等)。似乎是硬件/网络问题,而不是您的代码有任何问题(否则它永远不会适用于所有系统,对吧?)。
-
这也是一个 AIR 应用程序吗?如果是,可能会定期检查磁盘上的总字节数,您自己的代码可以决定下载是否完成(获取所有字节)并执行任何操作...如果 99.9% 表示缺少块,只需重新下载最终范围字节并附加到下载的完整字节。事实上:为什么不尝试进入更小的块,缝合部分(
file.append)以在磁盘上制作最终的大文件......