Lor*_*eck 2 air flash actionscript-3
无论如何使用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();
}
Run Code Online (Sandbox Code Playgroud)
另外要澄清一下,我的节目是adobe air.因此它将作为桌面应用程序运行,而不是网页上的Flash对象.
你必须使用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");
}
}
Run Code Online (Sandbox Code Playgroud)
我只是复制并粘贴了我的一些代码.不是100%完成,但应该指出你正确的方向......