Flex 文件上传导入组件


  上文讲到Flex利用Blazeds来上传文件,本文则封装了一个组件用于文件的上传导入,其主要功能有:
  1)文件的选择;
  2)文件类型的限定,比如只能选择txt文件,图片文件等;
  3)文件上传,利用Blazeds;
  4)文件导入,利用Blazeds;
  5)上传、导入的事件分发;
  1、文件上传组件核心代码  flex/spark"                      xmlns:mx="library://ns.adobe.com/flex/mx" width="300" height="28" creationComplete="init()">                                                                                          -->                                        [Event(name="result", type="event.FileImportEvent")]                    [Event(name="fault", type=" event.FileImportEvent")]                                        加载文件
  }
  /**
  * 设置文件过滤,打开文件选择框时刻选择哪些类型的文件
  * FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png")
  */ 
  public function set fileFilter(fileType:String):void{
  if(!StringUtil.isEmpty(fileType) && StringUtil.trim(fileType).length>3){
  _fileFilter = [];
  _fileFilter.push(new FileFilter("("+fileType+")",fileType));
  }
  }
  // Property: maxFileSize,Flash Player Limit 100 mb
  private var _maxFileSize:Number = 1024*1024*20;
  [Bindable]
  /**
  * 读取和设置文件最大尺寸,单位为Byte,1K = 1024Byte,默认值为20M
  */
  public function get maxFileSize():Number
  {
  return _maxFileSize;
  } 
  public function set maxFileSize(size:Number):void
  {
  _maxFileSize = size;
  }
  private function checkFile():Boolean{
  var blnResult:Boolean = true;
  if(StringUtil.isEmpty(txtFileInfo.text)){
  Alert.show("请选择要上传的文件!");
  return false;
  }
  if(file.size>_maxFileSize){    //文件大小验证失败
  Alert.show(StringUtil.format("该文件尺寸{0} 超出最大尺寸 {1}.",file.size/1000,_maxFileSize/1000));
  blnResult = false;
  }
  if(file.data==null){
  Alert.show("文件正在加载中,请稍等!");
  blnResult = false;
  }
  return blnResult;
  }
  protected function btnBrowse_clickHandler(event:MouseEvent):void
  {
  file.browse(_fileFilter);//打开文件选择框
  }
  protected function btnDelete_clickHandler(event:MouseEvent):void
  {
  //删除远程服务器的文件
  //清空txtFileInfo
  txtFileInfo.text = "";
  txtFileInfo.toolTip = "";
  }                               protected function ro_resultHandler(event:ResultEvent):void                             {                                      this.dispatchEvent(new FileImportEvent(FileImportEvent.RESULT,event.resul t));                             }                               protected function ro_faultHandler(event:FaultEvent):void                             {                                      this.dispatchEvent(new FileImportEvent(FileImportEvent.FAULT,null,event.f ault));                             }                                                          public function importFile(headers:Array):Boolean{                                      var isSuccess:Boolean = checkFile();                                      if(isSuccess){                                                ro.importFile(file.data,headers,file.type);                                      }                                      return isSuccess;                             }                                                          public function uploadFile():Boolean{                                      var isSuccess:Boolean = checkFile();                                      if(isSuccess){                                                ro.uploadFile(file.data,file.type);                                      }                                      return isSuccess;                             }                      ]]>                              
  
   2、FileImportEvent事件 package event {     import flash.events.Event;          import mx.rpc.Fault;       publicclass FileImportEvent extends Event     {        publicstaticconst RESULT:String      = "result";        publicstaticconst FAULT:String       = "fault";        //        privatevar _result:Object;        privatevar _fault:Fault;         //        publicfunction FileImportEvent(type:String, result:Object=null, fault:Fault=null)        {            super(type,true);            this._result = result;            this._fault = fault;        }        //        publicfunctionget result():Object{            return _result;        }        publicfunctionset result(value:Object):void{            _result = value;        }        //        publicfunctionget fault():Fault{            return _fault;        }        publicfunctionset fault(value:Fault):void{            _fault = value;        }     } } 3、示例  flex/spark"                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" creationComplete="init()" width="370" height="180"                xmlns:fileupload="whh.flex.controls.fileupload.*">                               
  
  
  
  
  

你可能感兴趣的:(Flex)