本文和大家重点讨论一下Flex上传文件功能如何实现,写过很多文件上传的功能,包括AJAX实现动态监控上传进度的,现在看到了实现Flex文件上传功能,还真是很方便,这里和大家分享一下。
Flex上传文件功能
写过很多文件上传的功能,包括AJAX实现动态监控上传进度的,现在看到了实现Flex文件上传功能,还真是很方便,没什么好说的,Flex上传文件代码:
upload.mxml
<?xmlversionxmlversion="1.0"encoding="utf-8"?>
<mx:Applicationxmlns:mxmx:Applicationxmlns:mx=http://www.adobe.com/2006/mxml
creationComplete="init()"layout="absolute"width="497"height="136"
backgroundGradientAlphas="[1.0,1.0]"backgroundGradientColors="[#F2F8F8,#45E7E5]">
<mx:Scriptsourcemx:Scriptsource="upload.as"></mx:Script>
<mx:Style>
.myfont{font-size:13pt}
</mx:Style>
<mx:Buttonxmx:Buttonx="10"y="95"label="上传文件"click="pickfile()"styleName="myfont"/>
<mx:Labelxmx:Labelx="10"y="10"text="文件上传"styleName="myfont"/>
<mx:ProgressBarxmx:ProgressBarx="10"y="40"width="457"themeColor="#F20D7A"minimum="0"
mode="manual"maximum="100"id="progress1"label="当前进度:0%"styleName="myfont"fontWeight="normal"/>
<mx:Labelxmx:Labelx="146"y="98"width="321"id="lbProgress"styleName="myfont"textAlign="right"/>
</mx:Application>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
upload.as
1//ActionScriptfile
2importflash.events.Event;
3importflash.net.FileFilter;
4importflash.net.FileReference;
5privatevarfileRef:FileReference=newFileReference();
6privatefunctioninit():void{
7
8}
9
10privatefunctionpickfile():void{
11varimageTypes:FileFilter=newFileFilter("图片(*.jpg,*.jpeg,*.gif,*.png)","*.jpg;*.jpeg;*.gif;*.png");
12vartextTypes:FileFilter=newFileFilter("文本文件(*.txt","*.txt;");
13varofficeType:FileFilter=newFileFilter("Office文件(*.doc,*.xls","*.doc;*.xls");
14varanyType:FileFilter=newFileFilter("所有文件(*.*)","*.*");
15varallTypes:Array=newArray(imageTypes,textTypes,officeType,anyType);
16fileRef.addEventListener(Event.SELECT,selectHandler);
17fileRef.addEventListener(Event.COMPLETE,completeHandler);
18fileRef.addEventListener(ProgressEvent.PROGRESS,progressHandler);
19fileRef.addEventListener("ioError",ioerrorHandler);
20try{
21varsuccess:Boolean=fileRef.browse(allTypes);
22}catch(error:Error){
23trace("Unabletobrowseforfiles."+error.toString());
24}
25}
26privatefunctionioerrorHandler(event:Event):void{
27trace("Unabletouploadfile."+event.toString());
28}
29privatefunctionprogressHandler(event:ProgressEvent):void{
30lbProgress.text="已上传"+(event.bytesLoaded/1024).toFixed(2)+"K,共"+(event.bytesTotal/1024).toFixed(2)+"K";
31varproc:uint=event.bytesLoaded/event.bytesTotal*100;
32progress1.setProgress(proc,100);
33progress1.label="当前进度:"+""+proc+"%";
34
35}
36privatefunctionselectHandler(event:Event):void{
37varrequest:URLRequest=newURLRequest("http://localhost:9080/upload/upload.jsp")
38try
39{
40fileRef.upload(request);
41}
42catch(error:Error)
43{
44trace("Unabletouploadfile."+error.toString());
45}
46}
47privatefunctioncompleteHandler(event:Event):void{
48trace("uploaded");
49}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
Flex上传文件效果图:
【编辑推荐】