当FileReference的browse()选择好图片后,如何得到已选择图片的路径?

利用DataEvent.UPLOAD_COMPLETE_DATA,当上载文件成功且从服务器接收数据之后派发此事件。

代码如下:

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                 xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                 xmlns:mx="library://ns.adobe.com/flex/mx"> 
  5.     <fx:Script> 
  6.         <![CDATA[  
  7.             public var file:FileReference;  
  8.               
  9.             public function selectFile():void   
  10.             {  
  11.                 file = new FileReference();  
  12.                 file.addEventListener(Event.SELECT, fileSelected);  
  13.                 file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);  
  14.                 file.addEventListener(Event.COMPLETE, uploadComplete);  
  15.                 file.addEventListener(IOErrorEvent.IO_ERROR, handleError);  
  16.                 file.browse();  
  17.             }      
  18.             public function handleError(event:IOErrorEvent):void   
  19.             {  
  20.                 status_txt.text = 'ERROR: ' + event.text + '';  
  21.             }  
  22.             public function fileSelected(event:Event):void  
  23.             {  
  24.                 file = FileReference(event.target);  
  25.                 file_txt.text = file.name;  
  26.                 status_txt.text = 'upload file: '+ file.name + '';          
  27.                 var request:URLRequest = new URLRequest();  
  28.                 request.url = "http://cn-pc-hz2166:9080/UploadServlet";  
  29.                 var headerVariables:URLVariables = new URLVariables();  
  30.                 headerVariables.fileName = file.name;  
  31.                 request.data = headerVariables;  
  32.                 request.method = URLRequestMethod.POST;  
  33.                 file.upload(request,"Filedata");              
  34.             }      
  35.             public function uploadDataComplete(event:DataEvent):void   
  36.             {  
  37.                 var result:XML = new XML(event.data);  
  38.                 status_txt.text += 'Upload Data Complete'  
  39.                 status_txt.text += 'RESULT: ' + result.toString() + ''  
  40.             }  
  41.               
  42.             public function uploadComplete(event:Event):void   
  43.             {  
  44.                 status_txt.text += 'Upload complete';  
  45.                   
  46.             }  
  47.         ]]> 
  48.     </fx:Script> 
  49.     <mx:VBox> 
  50.         <mx:TextInput id="file_txt"/> 
  51.         <mx:Button id="select_btn" label="select" click="selectFile();"/> 
  52.         <mx:TextArea id="status_txt" width="400" height="200"/> 
  53.     </mx:VBox> 
  54. </s:Application>