flex上传文件

      private var file:FileReference = new FileReference(); //上传下载类,


       //加一些窃听器事情
        file.addEventListener(ProgressEvent.PROGRESS, onProgress);
				file.addEventListener(Event.SELECT, file_select);
				file.addEventListener(Event.COMPLETE,onComplete);
				
				
		 /**
		    *上传文件打开选择文件框
		    * */
		   private function openDocWin():void   
		   {    
		    
		    var docFilter:FileFilter = new FileFilter("Documents", "*.png;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf"); //文件过滤类,过滤掉后缀不在这些的类
		    var allTypes:Array=new Array(docFilter);    
		    file.browse(allTypes);   //打开文件游览框
		   }

       /**
		    *进度条显示框
		    * */
         private var myBar:ProgressWindow= new ProgressWindow();
			private function onProgress(e: ProgressEvent): void{
				//弹出进度条
				if(flagProgress){
					PopUpManager.addPopUp(myBar,this,true);
					PopUpManager.centerPopUp(myBar);
				}
				flagProgress = false
				var proc: uint = e.bytesLoaded / e.bytesTotal * 100;
				myBar.setProGressBar(proc);
				var value:String="当前进度: " + " " + proc + "%";
				myBar.labelText=value ;
			}
			
			
			private function onComplete(e:Event):void{
				PopUpManager.removePopUp(myBar) ;
//				this.dispatchEvent( new CustomEvent("saveClick",null));
				loadAttachmentListByDataId(selectedItem.statuteId);
			}
			
			private function file_select (e:Event):void {
				if(file.name==null || file.name==""){
					stateText = "选择了文件 ";
				}else 
					stateText = file.name;
			}
			
			
       /**
		    *保存方法
		    * */
			protected function saveClickHandler(event:MouseEvent):void
			{
			if(file.size>DatabaseConstants.max){
							Message.showError("上传文件太大,限制在10M以内");
						}else{
							var request:URLRequest=HttpUtil.getRequest("bfStatuteFilesAction!savaStatute.do");
							var saveParam:URLVariables=new URLVariables();
							saveParam.tempId=selectedItem.statuteId;
							request.data = saveParam;
							request.method = URLRequestMethod.POST;
							try {
								file.upload(request,"file");
								stateText= "请选择一个文件上传";
							} catch (error:Error) {
								Message.showError("上传失败"+error);
							}
						}
			}
			


 

你可能感兴趣的:(flex上传文件)