Technorati 标签: ,,,,,

上网查了一些资料,摸索着用Flex写了一个 文件上传的工具。没做太多修饰,主要代码贴出来,免得以后忘了

   1:  <!-- filename :flashuploader.mxml -->
   2:  <?xml version="1.0" encoding="utf-8"?>
   3:  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
   4:                  xmlns:c="component.*"
   5:                  width="450" height="450" minWidth="460" minHeight="400" initialize="application1_initializeHandler(event)" layout="absolute">
   6:      <mx:Style source="css/main.css"/>
   7:      <mx:states>
   8:          <mx:State name="TEST">
   9:              <mx:AddChild position="lastChild" relativeTo="{filelist}">
  10:                  <c:uploadFile currentState="UploadError">
  11:                  </c:uploadFile>
  12:              </mx:AddChild>
  13:          </mx:State>
  14:      </mx:states>
  15:      <mx:LinkButton id="selectbutton" x="5" y="5" label="{selectlabel}" click="button1_clickHandler(event)"/>
  16:      <c:CountingVBox id="filelist" x="5" y="36" height="100%" maxCount="{max}"/>
  17:      <mx:Script>
  18:          <![CDATA[
  19:              import component.uploadFile;
  20:              import mx.events.FlexEvent;
  21:              private var filter:Array;
  22:              private var single:Boolean;
  23:              
  24:              private var fileRefs:FileReferenceList;
  25:              
  26:              private var fileRef:FileReference;
  27:              [Bindable]
  28:              private var max:int=10;
  29:              [Bindable]
  30:              private var selectlabel:String="文件上传";
  31:              
  32:              
  33:              
  34:              private function button1_clickHandler(event:MouseEvent):void
  35:              {
  36:                  if (single){
  37:                      fileRef = new FileReference();
  38:                      fileRef.addEventListener(Event.SELECT,singleSelectHandle);
  39:                      fileRef.browse(filter);
  40:                  }else{
  41:                      fileRefs = new FileReferenceList();
  42:                      fileRefs.addEventListener(Event.SELECT,multiSelectHandle);
  43:                      fileRefs.browse(filter);
  44:                  }
  45:              }
  46:              
  47:              private function singleSelectHandle(event:Event):void
  48:              {
  49:                  var file:uploadFile = new uploadFile();
  50:                  file.parentContainer=filelist;
  51:                  //                file.fileRef = FileReference(event.currentTarget);
  52:                  file.fileRef = fileRef;
  53:                  filelist.addChild(file);
  54:                  
  55:              }
  56:              
  57:              private function application1_initializeHandler(event:FlexEvent):void
  58:              {
  59:                  if( parameters.labelselect !=null) selectlabel = parameters.labelselect;
  60:                  if( parameters.ext != null)
  61:                      if ( parameters.type!=null)
  62:                          filter=[new FileFilter(parameters.type,parameters.ext)];
  63:                      else
  64:                          filter = [new FileFilter("Select Files"+parameters.ext, parameters.ext)];
  65:                  
  66:                  
  67:                  if (parameters.single!=null && "true" == parameters.single.toLowerCase()){
  68:                      single = true;
  69:                      max = 1;
  70:                  }else{
  71:                      if (parameters.max !=null )
  72:                          max = parameters.max ;
  73:                      if (isNaN(max)||max<=0 ) max=int.MAX_VALUE;
  74:                  }
  75:                  filelist.addEventListener("EMPTY",restoreSelectButton);
  76:                  filelist.addEventListener("FULL",disableSelectButton); 
  77:                  var prefix:String="http://localhost/fileupload";
  78:                  filelist.urlRequest = new URLRequest(prefix+"/upload.php");
  79:              }
  80:              
  81:              private function multiSelectHandle(event:Event):void
  82:              {    
  83:                  var fileList:Array=fileRefs.fileList;
  84:                  for(var i:int =0;i<fileList.length;i++){
  85:                      
  86:                      var file:uploadFile = new uploadFile();
  87:                      file.parentContainer=filelist;
  88:                      file.fileRef = fileList[i];
  89:                      filelist.addChild(file);
  90:                  }
  91:              }
  92:              
  93:              private function restoreSelectButton(event:Event):void
  94:              {
  95:                  selectbutton.visible=true;                
  96:              }
  97:              
  98:              private function disableSelectButton(event:Event):void
  99:              {
 100:                  selectbutton.visible=false;                
 101:                  
 102:              }
 103:              
 104:          ]]>
 105:      </mx:Script>    
 106:  </mx:Application>
   1:  <!-- filename: component/uploadFile.mxml -->
   2:  <?xml version="1.0" encoding="utf-8"?>
   3:  <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
   4:             xmlns:c="component.*"
   5:             height="20" width="440" initialize="canvas1_initializeHandler(event)" >
   6:      <mx:states>
   7:          <mx:State name="UploadError" >
   8:              <mx:SetProperty name="label" target="{progressbar1}" value="{errorInfo}"/>
   9:              <mx:SetProperty name="styleName" value="error"/>
  10:          </mx:State>
  11:          <mx:State basedOn="UploadError" name="FileError">
  12:              <mx:SetProperty name="x" target="{image1}" value="317"/>
  13:              <mx:RemoveChild target="{progressbar1}"/>
  14:              <mx:SetStyle name="styleName" target="{label1}" value="error1"/>
  15:              <mx:SetProperty name="toolTip" target="{label1}" value="{errorInfo}"/>
  16:          </mx:State>
  17:      </mx:states>
  18:      <mx:Image x="5" width="16" height="16" source="{fileicon}" verticalCenter="-1" />
  19:      <c:FilenameLabel x="23"  width="160" text="{filename}" truncSize="10" verticalCenter="-1"/>
  20:      <mx:Label x="185" width="64" text="{extname}" verticalCenter="-1"/>
  21:      <mx:Label id="label1" x="251" width="64" text="{filesize}" textAlign="right" verticalCenter="-1"/>
  22:      <mx:ProgressBar id="progressbar1" x="317" width="100" height="16" label="%3%%" labelPlacement="center" maximum="100" source="{_fileRef}"
  23:                      verticalCenter="-1"/>
  24:      <mx:Image id="image1" x="419" width="16" height="16" click="cancle_clickHandler(event)" source="@Embed('icons/cancel.png')" verticalCenter="-1"/>
  25:      <mx:Script>
  26:          <![CDATA[
  27:              import flash.events.MouseEvent;
  28:              import flash.events.ProgressEvent;
  29:              
  30:              import mx.controls.Alert;
  31:              import mx.events.FlexEvent;
  32:              import mx.states.Transition;
  33:              
  34:              private static var MAX_FILE_SIZE:uint =int.MAX_VALUE;
  35:              
  36:              private var _parent:CountingVBox;
  37:              [Bindable]
  38:              private var _fileRef:FileReference ;
  39:              [Bindable]
  40:              private var errorInfo:String="";
  41:              [Bindable]
  42:              private var fileicon:Class;
  43:              [Bindable]
  44:              private var extname:String;
  45:              [Bindable]
  46:              private var filesize:String;
  47:              [Bindable]
  48:              private var filename:String;
  49:              
  50:              
  51:              
  52:              
  53:              public function set fileRef(value:FileReference):void
  54:              {
  55:                  _fileRef = value;
  56:                  filename=_fileRef.name.substring(0,_fileRef.name.indexOf(_fileRef.type));
  57:                  extname=_fileRef.type;
  58:                  var size:uint=_fileRef.size;
  59:                  var unit:String="B";
  60:                  if(size>10240){
  61:                      size/=1024;
  62:                      unit="KB";
  63:                  }
  64:                  if(size>10240){
  65:                      size/=1024;
  66:                      unit="MB";
  67:                  }
  68:                  if(size>10240){
  69:                      size/=1024;
  70:                      unit="GB";
  71:                  }
  72:                  
  73:                  filesize=size.toFixed(0)+unit;
  74:                  fileicon=getFileIconClass();
  75:                  if (checkFilesize()){
  76:                      _fileRef.upload(_parent.urlRequest);
  77:                      _fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,upload_complete); 
  78:                }
  79:              }
  80:              
  81:              public function set parentContainer(p:CountingVBox):void
  82:              {
  83:                  _parent = p;
  84:              }
  85:              
  86:              
  87:              private function getFileIconClass():Class
  88:              {
  89:                  [Embed("icons/Unknown.png")] 
  90:                  var unknown:Class;
  91:                  
  92:                  [Embed("icons/doc.png")]
  93:                  var doc:Class;
  94:                                  
  95:                 //其他文件类型
  96:                  
  97:                  if (_fileRef.type == null){
  98:                      return unknown;
  99:                  }
 100:                  
 101:                  var type:String=_fileRef.type.toLowerCase();
 102:                  if (type == ".doc") return doc;
 103:                  if (type == ".docx") return doc;
 104:                  //其他文件类型
 105:                  
 106:                  return unknown;                
 107:              }
 108:              
 109:              private function cancle_clickHandler(event:MouseEvent):void
 110:              {
 111:                  _fileRef.cancel();
 112:                  _parent.removeUploadedFile(_fileRef);
 113:                  _parent.removeChild(this);
 114:                  if (_parent.count<1){
 115:                      _parent.dispatchEvent(new Event("EMPTY"));
 116:                  }
 117:              }  
 118:              
 119:              protected function upload_complete(event:DataEvent):void
 120:              {
 121:                  _parent.addUploadedFile(_fileRef); 
 122:              }
 123:              
 124:              protected function upload_IO_error(event:IOErrorEvent):void
 125:              {
 126:                  errorInfo="UploadError";
 127:                currentState = "UploadError";
 128:                   
 129:              }
 130:              
 131:              private function checkFilesize():Boolean
 132:              {
 133:                  if (_fileRef.size>MAX_FILE_SIZE){
 134:                      errorInfo = "File too Big";
 135:                      currentState = "FileError";
 136:                      return false;
 137:                  }
 138:                  return true;
 139:              }
 140:              
 141:          ]]>
 142:      </mx:Script>
 143:  </mx:Canvas>
 144: