Save a download without filereference

半世苍凉 提交于 2019-12-22 18:07:42

问题


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();
}

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.


回答1:


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");
    }
}

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




回答2:


I believe you might be able to do it if you use a packager like SWFStudio.

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



来源:https://stackoverflow.com/questions/8359107/save-a-download-without-filereference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!