心有林夕

魔鬼与天使同在

FLEX文件下载

文件下载没有文件上传那么麻烦,只需从服务器指定的目录获取即可。

 

<?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>

阅读更多
个人分类: Flex
想对作者说点什么? 我来说一句

Flex 指定文件下载 带进度条

2009年02月24日 297KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭