如何在基于Flex的AIR应用程序中从Internet上下载文件。
我尝试使用URL设置为地址的文件,但在尝试保存时出现了“文件不存在”的错误。而且很难通过谷歌搜索找到解决此问题的帮助。
如何在基于Flex的AIR应用程序中从Internet上下载文件。
我尝试使用URL设置为地址的文件,但在尝试保存时出现了“文件不存在”的错误。而且很难通过谷歌搜索找到解决此问题的帮助。
你需要从两个API组合中选择一个来完成此任务。
版本1是URLLoader和FileStream。
使用这两个类的组合,你可以通过URLLoader对象将文件从服务器加载到Air中。这将把文件下载到内存中,然后在下载完成时通知您。请确保使用URLLoaderDataFormat.BINARY数据格式初始化下载。然后,您需要初始化一个Filestream对象,并使用writeBytes()函数将其写入磁盘。
版本2是URLStream和FileStream。
URLStream与URLLoader非常相似,但它不需要在完全下载文件之后才使用结果。在下载过程中,数据就可以被提供给您使用。这种方法适用于大型文件,因为您不必等待完整的下载以开始将其保存到磁盘中,并且还可以节省内存,因为一旦播放器将其交给您,就可以释放相关数据的内存。您将以完全相同的方式使用filestream,只是在流式传输的每个文件块上执行writeBytes()函数。
var downloader = new FileDownloader("url", "Local/Path");
,然后调用downloader.load()
开始下载。package com.alex{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;
public class FileDownloader
{
// Class to download files from the internet
// Function called every time data arrives
// called with an argument of how much has been downloaded
public var onProgress :Function = function(t:uint):void{};
public var onComplete :Function = function():void{};
public var remotePath :String = "";
public var localFile :File = null;
private var stream :URLStream;
private var fileAccess :FileStream;
public function FileDownloader( remotePath :String = "" , localFile :File = null ) {
this.remotePath = remotePath;
this.localFile = localFile;
}
public function load() :void {
if( !stream || !stream.connected ) {
stream = new URLStream();
fileAccess = new FileStream();
var requester :URLRequest = new URLRequest( remotePath );
var currentPosition :uint = 0;
var downloadCompleteFlag :Boolean = false;
// Function to call oncomplete, once the download finishes and
// all data has been written to disc
fileAccess.addEventListener( "outputProgress", function ( result ) :void {
if( result.bytesPending == 0 && downloadCompleteFlag ) {
stream.close();
fileAccess.close();
onComplete();
}
});
fileAccess.openAsync( localFile, FileMode.WRITE );
stream.addEventListener( "progress" , function () :void {
var bytes :ByteArray = new ByteArray();
var thisStart :uint = currentPosition;
currentPosition += stream.bytesAvailable;
// ^^ Makes sure that asyncronicity does not break anything
stream.readBytes( bytes, thisStart );
fileAccess.writeBytes( bytes, thisStart );
onProgress( currentPosition );
});
stream.addEventListener( "complete", function () :void {
downloadCompleteFlag = true;
});
stream.load( requester );
} else {
// Do something unspeakable
}
}
}}
downloadFile: function (url, fileName) {
var urlStream = new air.URLStream();
var request = new air.URLRequest(url);
var fileStream = new air.FileStream();
// write 50k from the urlstream to the filestream, unless
// the writeAll flag is true, when you write everything in the buffer
function writeFile(writeAll) {
if (urlStream.bytesAvailable > 51200 || writeAll) {
alert("got some");
var dataBuffer = new air.ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
// do clean up:
if (writeAll) {
alert("done");
fileStream.close();
urlStream.close();
// set up the next download
setTimeout(this.downloadNextFile.bind(this), 0);
}
}
urlStream.addEventListener(air.Event.COMPLETE, writeFile.bind(this, true));
urlStream.addEventListener(air.ProgressEvent.PROGRESS, writeFile.bind(this, false));
var file = air.File.desktopDirectory.resolvePath(fileName);
fileStream.openAsync(file, air.FileMode.WRITE);
urlStream.load(request);
}
请查看flash.net.URLRequest
类,它将帮助您下载文件。