【发布时间】:2012-03-07 21:38:06
【问题描述】:
我使用了这里的示例 Download a file with Adobe AIR 并构建了从服务器下载文件的应用程序。
我将尝试逐步解释错误。
1) Adobe Air 应用程序从服务器 http://example.com/data/init.xml 下载 xml 文件
2) 我已经打开了,一切正常。
3) Adobe Air 应用程序再次从服务器下载了相同的文件。现在,如果我用记事本打开它,它会说 init.xml 是二进制文件。如果我从磁盘中删除 init.xml 并重试 - 一样。 init.xml 是一个二进制文件。重新打开空气应用程序不起作用。
4) 我将服务器上的 init.xml 更改为 init123.xml 并再次下载。 init123.xml 作为普通 xml 文件打开。如果我再次下载它,那么第 3 步 - init123.xml 是一个二进制文件。
错误可能在哪里?
谢谢。
操作系统 - Windows 7
文件的 MD5 也发生了变化。
这可以解决,我在 url 的末尾添加随机数。
urlStream.load(new URLRequest(remoteFile+'?'+Math.random()));
但是这个
urlStream.load(new URLRequest(remoteFile));
如果我第二次加载它会使文件二进制。
来源
private function startDownloading():void
{
destFile.nativePath = destDirectory +destFileBase;
fileStream = new FileStream();
fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, outputProgress);
fileStream.addEventListener(IOErrorEvent.IO_ERROR, fileAccessError);
fileStream.openAsync(destFile, FileMode.WRITE);
urlStream = new URLStream();
urlStream.addEventListener(ProgressEvent.PROGRESS, progress);
urlStream.addEventListener(Event.COMPLETE, complete);
urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, urlStreamSecurityError);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, urlStreamIOError);
urlStream.load(new URLRequest(remoteFile));
}
protected function fileAccessError(event:IOErrorEvent):void
{
urlStream.close();
fileStream.close();
}
protected function outputProgress(event:OutputProgressEvent):void
{
if(event.bytesPending == 0 && downloadCompleteFlag ) {
}
}
protected function urlStreamIOError(event:IOErrorEvent):void
{
trace('error 2');
}
protected function urlStreamSecurityError(event:SecurityErrorEvent):void
{
trace('error 2');
}
protected function progress(event:ProgressEvent):void
{
var bytes :ByteArray = new ByteArray();
var thisStart :uint = currentPosition;
currentPosition += urlStream.bytesAvailable;
urlStream.readBytes( bytes, thisStart );
fileStream.writeBytes( bytes, thisStart );
}
protected function complete(event:Event):void
{
urlStream.close();
fileStream.close();
downloadCompleteFlag = true;
}
【问题讨论】:
-
您为什么使用 URLStream 而不是 URLLoader?我不知道 Stream 相关的类,但是 URLLoader 可以让你指定数据格式。
-
你能提供一些代码吗?您用来保存文件的代码也可能很有用。
-
您没有报告看到任何 'error 2',所以我想没有。你能追踪 currentPosition 和 urlStream.bytesAvailable 吗?
-
您确定开始下载时目标文件为空吗?
-
感谢您的回答。它看起来像带有缓存的东西。因为,有了这个新的 URLRequest(remoteFile+'?'+Math.random()) 可以完美地工作。如果我能解决这个问题,我会尝试在不同的操作系统上进行实验。
标签: actionscript-3 apache-flex air