[ActionScript3.0] 使用FileReferenceList处理多个文件上载

  1 package

  2 {

  3     import flash.display.Sprite;

  4     import flash.events.DataEvent;

  5     import flash.events.Event;

  6     import flash.events.MouseEvent;

  7     import flash.events.ProgressEvent;

  8     import flash.net.FileFilter;

  9     import flash.net.FileReference;

 10     import flash.net.FileReferenceList;

 11     import flash.net.URLRequest;

 12     import flash.net.URLRequestMethod;

 13     import flash.net.URLVariables;

 14     import flash.text.TextField;

 15     import flash.text.TextFieldAutoSize;

 16     /**

 17     * ...

 18     * @author Frost.Yen

 19     */

 20     public class UploadListAS3 extends Sprite

 21     {

 22         private var _label:TextField; 

 23         private var _button:TextField;

 24         private var _fileRefer:FileReferenceList; 

 25         private var _count:int = 0;

 26         private var _request:URLRequest = new URLRequest("http://localhost/yanzimen/");

 27         public function UploadListAS3()

 28         {

 29             if(stage) init();

 30             else addEventListener(Event.ADDED_TO_STAGE,init);

 31         }

 32         private function init(e:Event = null):void

 33         {

 34             _label = new TextField(); 

 35             _label.text = "please select file"; 

 36             _label.autoSize = TextFieldAutoSize.LEFT;

 37             _label.background = true;

 38             _label.width = 300;

 39             _label.wordWrap = true;

 40             _label.backgroundColor = 0xffffff;

 41             _label.border = true;

 42             _button = new TextField();

 43             _button.text = "click here to select file";

 44             _button.autoSize = TextFieldAutoSize.LEFT; 

 45             _button.selectable = false;

 46             _button.background = true;

 47             _button.backgroundColor = 0xE29966;

 48             _button.border = true;

 49             this.addChild(_label); 

 50             this.addChild(_button); 

 51             _button.x = 310;

 52    

 53             _fileRefer = new FileReferenceList();

 54             _button.addEventListener(MouseEvent.CLICK,toSelect);

 55             _fileRefer.addEventListener(Event.SELECT,onSelect);

 56             _fileRefer.addEventListener(Event.CANCEL,onCancel);

 57         }

 58         private function toSelect(e:MouseEvent):void

 59         {

 60             _fileRefer.browse(); //browse(typeFilter:Array=null) 使用 typeFilter 参数,可决定对话框显示哪些文件。

 61             _button.removeEventListener(MouseEvent.CLICK,toSelect); 

 62         }

 63         private function onProgress(e:ProgressEvent):void

 64         {

 65             _label.text = Number(Math.round(e.bytesLoaded / e.bytesTotal*100))+"%";

 66             //label.text = e.bytesLoaded+"/"+e.bytesTotal;

 67         }

 68         private function onComplete(e:Event):void

 69         {

 70             e.target.removeEventListener(Event.COMPLETE,onComplete);

 71             e.target.removeEventListener(ProgressEvent.PROGRESS,onProgress);

 72    

 73             if(_count<_fileRefer.fileList.length-1){

 74                 _label.text = "load complete "+(_count+1)+" file";

 75                 _count++;

 76             }else{

 77                 _count=0;

 78                 _button.addEventListener(MouseEvent.CLICK,toUpload);

 79                 _label.text = "all files load complete!";

 80                 _button.text = "to upload";

 81             }

 82         }

 83         private function toUpload(e:MouseEvent):void{

 84             onUpLoad(_fileRefer.fileList[_count]);

 85         }

 86         private function onSelect(e:Event):void

 87         {

 88             _label.text = "";

 89             for(var i:int = 0;i<_fileRefer.fileList.length;i++){

 90                 trace("the name of the selected file:",_fileRefer.fileList[i].name);

 91                 _label.appendText("file "+(i+1)+":"+_fileRefer.fileList[i].name+"\n");

 92                 _fileRefer.fileList[i].addEventListener(Event.COMPLETE,onComplete);

 93                 _fileRefer.fileList[i].addEventListener(ProgressEvent.PROGRESS,onProgress);

 94                 (_fileRefer.fileList[i] as FileReference).load();

 95             }

 96         }

 97         private function onCancel(e:Event):void

 98         {

 99             trace("cancel to upload");

100             _button.addEventListener(MouseEvent.CLICK,toSelect); 

101         }

102         /**

103          * 调用FileReference的实例方法upload()实现文件上传

104          * @param    file

105          */

106         private function onUpLoad(file:FileReference):void

107         {

108             _label.text = "file "+(_count+1)+" size"+Number(file.size/1024/1024).toFixed(2)+"M";

109             var variables:URLVariables = new URLVariables();

110             variables.fileName = file.name;

111             _request.data = variables;

112             _request.method = URLRequestMethod.POST;

113             file.addEventListener(ProgressEvent.PROGRESS, progressHandler);

114             file.addEventListener(Event.COMPLETE, completeHandler);

115             file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeDataHandler);

116             try

117             {

118                 file.upload(_request);

119             } 

120             catch(error:Error) 

121             {

122                 trace(error.message);

123             }

124    

125             function progressHandler(e:ProgressEvent):void

126             {

127                 _label.text = "file "+(_count+1)+" uploading "+Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";

128             }

129             function completeHandler(e:Event):void

130             {

131                 var file:FileReference = FileReference(e.target);

132                 file.removeEventListener(ProgressEvent.PROGRESS, progressHandler);

133                 file.removeEventListener(Event.COMPLETE, completeHandler);

134                 _button.removeEventListener(MouseEvent.CLICK,toUpload);

135                 _button.addEventListener(MouseEvent.CLICK,toSelect);

136                 

137                 if(_count<_fileRefer.fileList.length-1){

138                     _label.text = "upload complete "+(_count+1)+" file";

139                     _count++;

140                     onUpLoad(_fileRefer.fileList[_count]);

141                 }else{

142                     _label.text = "all files upload complete";

143                     _button.text = "click here to select file";

144                 }

145             }

146             function completeDataHandler(e:DataEvent):void

147             {

148                 var file:FileReference = FileReference(e.target);

149                 file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeHandler);

150                 if(_count==_fileRefer.fileList.length-1){

151                     trace("upload_complete_data",e.data);//上传成功后,从后台返回的数据

152                 }

153             }

154         }

155         /**

156          * FileFilter 类用于表示在调用 FileReference.browse() 方法、FileReferenceList.browse() 方法或调用 File、FileReference 或 FileReferenceList 对象的 browse 方法时显示的文件浏览对话框中显示用户系统上的哪些文件。

157          * @return

158          */

159         private function getImageTypeFilter():FileFilter {

160             return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");

161         }

162       

163         private function getTextTypeFilter():FileFilter {

164             return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");

165         }

166     }

167 }

 

你可能感兴趣的:(actionscript)