技术开发 频道

Flex 2.0 从零开始实现文件上传

8. 前台的FileUpload.mxml文件代码如下:
 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns="*" creationComplete="init()">

<mx:Script>
<![CDATA[
import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;

private var currentAction:String;
private var uploadURL:URLRequest;
private var file:FileReference;

private var fileName:String;

private function init() : void{
file = new FileReference();
}

public function FileReference_browse() : void {
currentAction = "upload";
uploadURL = new URLRequest();
file = new FileReference();
configureListeners(file);
file.browse();
}

private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.SELECT, selectHandler);
}

private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
if(currentAction == "upload"){
uploadURL.url = "myUpload?path=work&filename=" + file.name;
file.upload(uploadURL);
}
}

]]>
</mx:Script>

<mx:Panel width="100%" height="100%">
<mx:VBox width="100%" horizontalAlign="center">
<mx:Label text="Click the below button to select a file which you want to upload!" />
<mx:Button label="Upload" click="FileReference_browse()" />
</mx:VBox>
</mx:Panel>

</mx:Application>




 


 




9. 开启tomcat,运行。大功告成!
 

0
相关文章