【问题标题】:Save a download without filereference保存没有文件引用的下载
【发布时间】:2011-12-02 16:05:34
【问题描述】:

是否可以使用 URLLoader 下载文件,然后将其保存到磁盘而不使用文件引用或任何使用对话框的东西?这是我拥有但不起作用的:

public function onDownloadComplete(e:Event):void
{
    DownloaderProgress.label = 'Download Done';

    var loader:URLLoader = URLLoader(e.target);
    var rtsFile:File = File.applicationStorageDirectory.resolvePath("RTS.zip");
    var rtsStream:FileStream = new FileStream();
    rtsStream.open(rtsFile, FileMode.WRITE);
    rtsStream.writeBytes(loader.data);
    rtsStream.close();
}

另外澄清一下,我的程序是在 adobe air 中。因此它将作为桌面应用程序而不是网页上的 Flash 对象运行。

【问题讨论】:

  • navigatetoURL(new URLRequest('RTS.zip'));
  • 应该是。我的意思是,你可以在不需要对话框的情况下写入文件,为什么你不能写入下载的字节?
  • @LordZardeck 你的意思是上传
  • mgraph,即使我在浏览器中也无法正常工作。它仍然会显示一个对话框询问保存位置(取决于用户的设置)
  • @mgraph 不,我的意思是下载。我的程序的目的是为用户管理安装一个复杂的程序。它需要下载数据文件,然后提取它们。稍后我会担心提取,我只是想下载东西。

标签: flash actionscript-3 air


【解决方案1】:

你必须使用 URLStream 而不是 URLLoader:

var downloadStream:URLStream = new URLStream();
downloadStream.addEventListener(Event.COMPLETE, onDownloadComplete);
downloadStream.load(new URLRequest(url));

// ...

private function onDownloadComplete(event:Event):void
{
    var bytes:ByteArray = new ByteArray();
    downloadStream.readBytes(bytes);

    try
    {           
        // delete old file first
        if (_saveToFile.exists)
        {
            _saveToFile.deleteFile();
        }
        _fs = new FileStream();
        _fs.addEventListener(IOErrorEvent.IO_ERROR, onSaveFileIOError);
        _fs.open(_saveToFile, FileMode.WRITE);
        _fs.writeBytes(bytes);
        _fs.close();

        _fs.removeEventListener(IOErrorEvent.IO_ERROR, onSaveFileIOError);
    }
    catch (error:Error)
    {
        trace("could not write file to local machine");
    }
}

我只是从我的一类中复制并粘贴了一些代码。不是 100% 完成,但应该为您指明正确的方向...

【讨论】:

  • 太棒了!效果很好!我已经搜索了 50 多个不同的搜索引擎页面,但找不到答案!
【解决方案2】:

如果您使用像 SWFStudio 这样的打包程序,我相信您可能能够做到。

http://www.northcode.com/forums/showthread.php?t=10108&highlight=download

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    相关资源
    最近更新 更多