当FileReference的browse()选择好图片后,如何得到已选择图片的路径?
利用DataEvent.UPLOAD_COMPLETE_DATA,当上载文件成功且从服务器接收数据之后派发此事件。
代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx">
- <fx:Script>
- <![CDATA[
- public var file:FileReference;
- public function selectFile():void
- {
- file = new FileReference();
- file.addEventListener(Event.SELECT, fileSelected);
- file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
- file.addEventListener(Event.COMPLETE, uploadComplete);
- file.addEventListener(IOErrorEvent.IO_ERROR, handleError);
- file.browse();
- }
- public function handleError(event:IOErrorEvent):void
- {
- status_txt.text = 'ERROR: ' + event.text + '';
- }
- public function fileSelected(event:Event):void
- {
- file = FileReference(event.target);
- file_txt.text = file.name;
- status_txt.text = 'upload file: '+ file.name + '';
- var request:URLRequest = new URLRequest();
- request.url = "http://cn-pc-hz2166:9080/UploadServlet";
- var headerVariables:URLVariables = new URLVariables();
- headerVariables.fileName = file.name;
- request.data = headerVariables;
- request.method = URLRequestMethod.POST;
- file.upload(request,"Filedata");
- }
- public function uploadDataComplete(event:DataEvent):void
- {
- var result:XML = new XML(event.data);
- status_txt.text += 'Upload Data Complete'
- status_txt.text += 'RESULT: ' + result.toString() + ''
- }
- public function uploadComplete(event:Event):void
- {
- status_txt.text += 'Upload complete';
- }
- ]]>
- </fx:Script>
- <mx:VBox>
- <mx:TextInput id="file_txt"/>
- <mx:Button id="select_btn" label="select" click="selectFile();"/>
- <mx:TextArea id="status_txt" width="400" height="200"/>
- </mx:VBox>
- </s:Application>