保存下载没有的FileReference [英] Save a download without filereference

查看:198
本文介绍了保存下载没有的FileReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是有下载与URLLoader一个文件,然后将其保存到磁盘,而无需使用一个使用对话框FileReference或什么吗?这是我有,但不工作:

Is there anyway to download a file with the URLLoader and then save it to the disk without using filereference or anything that uses a dialog? This is what I have but isn't working:

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对象。

Also to clarify, My program is in adobe air. So it will be ran as a desktop application not a flash object on a webpage.

推荐答案

您必须使用替代的URLLoader使用URLStream:

you have to use the URLStream instead of the 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");
    }
}

我只是复制和;从粘贴一类我的一些code。不完整的100%,但应该指向你在正确的方向...

i just copy&pasted some code from a class of mine. not 100% complete but should point you in the right direction...

这篇关于保存下载没有的FileReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆