文件下载没有文件上传那么麻烦,只需从服务器指定的目录获取即可。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
backgroundColor="#ffffff">
<mx:Style source="styles/styles.css" />
<mx:HRule x="10" y="37" width="90%"/>
<mx:Text x="10" y="10" text="Downloading a File" styleName="headerStyle" id="label1"/>
<mx:Button x="10" y="75" label="Download HTML Component" click="{download()}"/>
<mx:Script>
<![CDATA[
import flash.events.*;
import flash.events.DataEvent;
import mx.events.CloseEvent;
import mx.controls.Alert;
import flash.net.navigateToURL;
public function download():void {
var url:String = encodeURI("http://localhost:8080/php/UpAndDownLoad/php/upload/images/qqpic.jpg");//encodeURI用来处理有中文的文件路径
var request:URLRequest = new URLRequest(url);
var fileRef:FileReference = new FileReference();
configureListeners(fileRef);
fileRef.download(request);
//navigateToURL(new URLRequest("http://localhost:8080/php/UpAndDownLoad/php/upload/images/qqpic.jpg"), "_blank");
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
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);
}
public function ioErrorHandler(event:Event):void{
trace("io error occured ");
}
public function selectHandler(event:Event):void{
trace("selectHandler");
}
public function openHandler(event:Event):void{
trace("File opened");
}
public function progressHandler(event:ProgressEvent):void{
trace("File download in progress (" + event.bytesLoaded + " of " + event.bytesTotal + ")");
}
public function cancelHandler(event:Event):void{
trace("cancelHandler");
}
public function completeHandler(event:Event):void{
trace("File downloaded");
}
]]>
</mx:Script>
</mx:Application>