1 package 2 { 3 import flash.display.SimpleButton; 4 import flash.display.Sprite; 5 import flash.errors.IllegalOperationError; 6 import flash.errors.MemoryError; 7 import flash.events.DataEvent; 8 import flash.events.Event; 9 import flash.events.MouseEvent; 10 import flash.events.ProgressEvent; 11 import flash.net.FileFilter; 12 import flash.net.FileReference; 13 import flash.net.URLRequest; 14 import flash.net.URLRequestMethod; 15 import flash.net.URLVariables; 16 import flash.text.TextField; 17 import flash.text.TextFieldAutoSize; 18 /** 19 * ... 20 * @author Frost.Yen 21 */ 22 public class UploadAS3 extends Sprite 23 { 24 private var _label:TextField; 25 private var _button:TextField; 26 private var _fileRefer:FileReference; 27 private var _request:URLRequest = new URLRequest("http://localhost/yanzimen/"); 28 public function UploadAS3() 29 { 30 if(stage) init(); 31 else addEventListener(Event.ADDED_TO_STAGE,init); 32 } 33 private function init(e:Event = null):void 34 { 35 _label = new TextField(); 36 _label.text = "please select file"; 37 _label.autoSize = TextFieldAutoSize.LEFT; 38 _label.background = true; 39 _label.width = 300; 40 _label.wordWrap = true; 41 _label.backgroundColor = 0xffffff; 42 _label.border = true; 43 _button = new TextField(); 44 _button.text = "click here to select file"; 45 _button.autoSize = TextFieldAutoSize.LEFT; 46 _button.selectable = false; 47 _button.background = true; 48 _button.backgroundColor = 0xE29966; 49 _button.border = true; 50 this.addChild(_label); 51 this.addChild(_button); 52 _button.x = 310; 53 54 _fileRefer = new FileReference(); 55 _button.addEventListener(MouseEvent.CLICK,toSelect); 56 _fileRefer.addEventListener(Event.SELECT,onSelect); 57 _fileRefer.addEventListener(Event.CANCEL,onCancel); 58 } 59 private function toSelect(e:MouseEvent):void 60 { 61 _fileRefer.browse(); //browse(typeFilter:Array=null) 使用 typeFilter 参数,可决定对话框显示哪些文件。 62 _button.removeEventListener(MouseEvent.CLICK,toSelect); 63 } 64 private function onProgress(e:ProgressEvent):void 65 { 66 _label.text = Number(Math.round(e.bytesLoaded / e.bytesTotal*100))+"%"; 67 //label.text = e.bytesLoaded+"/"+e.bytesTotal; 68 } 69 private function onComplete(e:Event):void 70 { 71 _fileRefer.removeEventListener(Event.COMPLETE,onComplete); 72 _fileRefer.removeEventListener(ProgressEvent.PROGRESS,onProgress); 73 _button.addEventListener(MouseEvent.CLICK,toUpload); 74 _label.text = "load complete!"; 75 _button.text = "upload"; 76 } 77 private function toUpload(e:MouseEvent):void{ 78 onUpLoad(_fileRefer); 79 } 80 private function onSelect(e:Event):void 81 { 82 trace("the name of the selected file:",(e.target as FileReference).name); 83 _label.text = "file name:"+(e.target as FileReference).name; 84 _fileRefer.addEventListener(Event.COMPLETE,onComplete); 85 _fileRefer.addEventListener(ProgressEvent.PROGRESS,onProgress); 86 _fileRefer.load(); 87 } 88 private function onCancel(e:Event):void 89 { 90 trace("cancel to upload"); 91 _button.addEventListener(MouseEvent.CLICK,toSelect); 92 } 93 /** 94 * 调用FileReference的实例方法upload()实现文件上传 95 * @param file 96 */ 97 private function onUpLoad(file:FileReference):void 98 { 99 _label.text = "file size"+Number(file.size/1024/1024).toFixed(2)+"M"; 100 var variables:URLVariables = new URLVariables(); 101 variables.fileName = file.name; 102 _request.data = variables; 103 _request.method = URLRequestMethod.POST; 104 file.addEventListener(ProgressEvent.PROGRESS, progressHandler); 105 file.addEventListener(Event.COMPLETE, completeHandler); 106 file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeDataHandler); 107 try 108 { 109 file.upload(_request); 110 } 111 catch(error:Error) 112 { 113 trace(error.message); 114 } 115 116 function progressHandler(e:ProgressEvent):void 117 { 118 _label.text = "uploading "+Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%"; 119 } 120 function completeHandler(e:Event):void 121 { 122 var file:FileReference = FileReference(e.target); 123 file.removeEventListener(ProgressEvent.PROGRESS, progressHandler); 124 file.removeEventListener(Event.COMPLETE, completeHandler); 125 _button.removeEventListener(MouseEvent.CLICK,toUpload); 126 _button.addEventListener(MouseEvent.CLICK,toSelect); 127 _label.text = "upload complete"; 128 _button.text = "click here to select file"; 129 } 130 function completeDataHandler(e:DataEvent):void 131 { 132 var file:FileReference = FileReference(e.target); 133 file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeHandler); 134 trace(e.data);//上传成功后,从后台返回的数据 135 } 136 } 137 /** 138 * FileFilter 类用于表示在调用 FileReference.browse() 方法、FileReferenceList.browse() 方法或调用 File、FileReference 或 FileReferenceList 对象的 browse 方法时显示的文件浏览对话框中显示用户系统上的哪些文件。 139 * @return 140 */ 141 private function getImageTypeFilter():FileFilter { 142 return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png"); 143 } 144 145 private function getTextTypeFilter():FileFilter { 146 return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf"); 147 } 148 } 149 }