flex远程下载文件实例(进阶版)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private var downloadURL:URLRequest;
private var netFile:FileReference;
public function download(netFileURL:String):void{
downloadURL = new URLRequest(netFileURL);
netFile = new FileReference();
configureListeners(netFile);
netFile.download(downloadURL);
}
private function configureListeners(dispatcher:IEventDispatcher):void{
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); //当由于安全错误导致下载失败时调度
dispatcher.addEventListener(Event.SELECT, selectHandler); //当用户从对话框中选择要下载的文件时调度
dispatcher.addEventListener(Event.OPEN, openHandler); // 当下载操作开始时调度
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); //在文件下载操作期间进行定期调度
dispatcher.addEventListener(Event.CANCEL, cancelHandler); //当用户取消对话框时调度
dispatcher.addEventListener(Event.COMPLETE, completeHandler); //当文件下载操作成功完成时调度
}
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("文件网络地址:" + downloadURL.url);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("文件本地化后的名称:" + file.name);
trace("已下载字节数:" + event.bytesLoaded);
trace("文件总字节数:" + event.bytesTotal);
trace("文件下载到本地的日期:" + file.creationDate); //Fri Jan 15 09:15:29 GMT+0800 2010
trace("文件格式:" + file.type);
trace("到本地时的文件大小:" + file.size);
}
private function cancelHandler(event:Event):void{
trace("cancelHandler: " + event);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("错误信息: " + event);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
]]>
</mx:Script>
<mx:Button x="300" y="50" label="远程文件下载"
click="download('http://www.google.cn/intl/zh-CN/images/logo_cn.gif');"/>
</mx:Application>