首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Adobe Air。URLStream或FileStream的奇怪错误

Adobe Air。URLStream或FileStream的奇怪错误
EN

Stack Overflow用户
提问于 2012-02-16 21:28:45
回答 3查看 2.5K关注 0票数 1

我使用了这里的示例Download a file with Adobe AIR,并构建了从服务器下载文件的应用程序。

我将尝试一步一步地解释错误。

1) Adobe Air app从服务器http://example.com/data/init.xml下载xml文件

2)我已经打开了,一切正常。

3) Adobe Air应用程序再次从服务器下载相同的文件。现在,如果我用记事本打开它,它会显示init.xml是二进制文件。如果我从磁盘中删除init.xml并重试-同样如此。init.xml是一个二进制文件。Reopen air应用程序不起作用。

4)我将服务器上的init.xml更改为init123.xml并重新下载。init123.xml被作为普通的xml文件打开。如果我再次下载它,那么步骤3- init123.xml是一个二进制文件。

错误可能出在哪里?

谢谢。

操作系统- Windows 7

文件的MD5也已更改。

如果我在url的末尾添加随机数,这个问题就可以解决。

代码语言:javascript
复制
urlStream.load(new URLRequest(remoteFile+'?'+Math.random()));

但这是

代码语言:javascript
复制
urlStream.load(new URLRequest(remoteFile));

如果我第二次加载文件,则将其设置为二进制。

来源

代码语言:javascript
复制
    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;
    }
EN

回答 3

Stack Overflow用户

发布于 2012-02-16 21:36:39

请尝试清空目标文件。您可能会将前面的字节保留在init.xml中。

票数 1
EN

Stack Overflow用户

发布于 2012-02-17 02:06:54

您正在错误的位置对ByteArray进行读写操作。您必须:

代码语言:javascript
复制
// In "progress" function
urlStream.readBytes( bytes, thisStart );
fileStream.writeBytes( bytes, thisStart );

其中,thisStarturlStream中当前可用的字节数。

当使用URLStream.readBytes()时,position指的是你想要开始读入bytes的位置。同样,FileStream.writeBytes()中的position参数表示要开始从bytes读取的位置。由于每次调用progress()时都会重新实例化bytes,因此应该始终从0位置开始读取和写入。

票数 0
EN

Stack Overflow用户

发布于 2012-02-17 02:29:24

据我所知,图像正在被缓存,这将导致文件的即时加载。

您的代码不会处理这一点,因为在您的完整函数中,您没有执行fileStream.writeBytes,这将导致一个空文件。

这不是经过测试的代码,但您需要这样做。

代码语言:javascript
复制
protected function progress(event:ProgressEvent):void
{
  var bytes :ByteArray = new ByteArray();
  var thisStart :uint = currentPosition;
  currentPosition += urlStream.bytesAvailable;
  urlStream.readBytes(bytes, thisStart );
  if(bytes.length > 0){
    fileStream.writeBytes( bytes, thisStart );
  }
}       
protected function complete(event:Event):void
{
  progress(event);// call progress to verify data is written
  urlStream.close();
  fileStream.close();
  downloadCompleteFlag = true;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9312133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档