学习笔记 剖析Flex上传文件功能如何实现

开发 后端
Flex有很多值得学习的地方,本文向大家介绍一下Flex上传文件功能,相信本文介绍一定会让你有所收获,欢迎大家一起来学习。

本文和大家重点讨论一下Flex上传文件功能如何实现,写过很多文件上传的功能,包括AJAX实现动态监控上传进度的,现在看到了实现Flex文件上传功能,还真是很方便,这里和大家分享一下。

Flex上传文件功能

写过很多文件上传的功能,包括AJAX实现动态监控上传进度的,现在看到了实现Flex文件上传功能,还真是很方便,没什么好说的,Flex上传文件代码:

upload.mxml

  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
  2. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx=http://www.adobe.com/2006/mxml
  3. creationComplete="init()"layout="absolute"width="497"height="136"
  4. backgroundGradientAlphas="[1.0,1.0]"backgroundGradientColors="[#F2F8F8,#45E7E5]"> 
  5. <mx:Scriptsourcemx:Scriptsource="upload.as"></mx:Script> 
  6. <mx:Style> 
  7. .myfont{font-size:13pt}  
  8. </mx:Style> 
  9. <mx:Buttonxmx:Buttonx="10"y="95"label="上传文件"click="pickfile()"styleName="myfont"/> 
  10. <mx:Labelxmx:Labelx="10"y="10"text="文件上传"styleName="myfont"/> 
  11. <mx:ProgressBarxmx:ProgressBarx="10"y="40"width="457"themeColor="#F20D7A"minimum="0"
  12. mode="manual"maximum="100"id="progress1"label="当前进度:0%"styleName="myfont"fontWeight="normal"/> 
  13. <mx:Labelxmx:Labelx="146"y="98"width="321"id="lbProgress"styleName="myfont"textAlign="right"/> 
  14. </mx:Application> 
  15.  

 upload.as

  1. 1//ActionScriptfile  
  2. 2importflash.events.Event;  
  3. 3importflash.net.FileFilter;  
  4. 4importflash.net.FileReference;  
  5. 5privatevarfileRef:FileReference=newFileReference();  
  6. 6privatefunctioninit():void{  
  7. 7  
  8. 8}  
  9. 9  
  10. 10privatefunctionpickfile():void{  
  11. 11varimageTypes:FileFilter=newFileFilter("图片(*.jpg,*.jpeg,*.gif,*.png)","*.jpg;*.jpeg;*.gif;*.png");  
  12. 12vartextTypes:FileFilter=newFileFilter("文本文件(*.txt","*.txt;");  
  13. 13varofficeType:FileFilter=newFileFilter("Office文件(*.doc,*.xls","*.doc;*.xls");  
  14. 14varanyType:FileFilter=newFileFilter("所有文件(*.*)","*.*");  
  15. 15varallTypes:Array=newArray(imageTypes,textTypes,officeType,anyType);  
  16. 16fileRef.addEventListener(Event.SELECT,selectHandler);  
  17. 17fileRef.addEventListener(Event.COMPLETE,completeHandler);  
  18. 18fileRef.addEventListener(ProgressEvent.PROGRESS,progressHandler);  
  19. 19fileRef.addEventListener("ioError",ioerrorHandler);  
  20. 20try{  
  21. 21varsuccess:Boolean=fileRef.browse(allTypes);  
  22. 22}catch(error:Error){  
  23. 23trace("Unabletobrowseforfiles."+error.toString());  
  24. 24}  
  25. 25}  
  26. 26privatefunctionioerrorHandler(event:Event):void{  
  27. 27trace("Unabletouploadfile."+event.toString());  
  28. 28}  
  29. 29privatefunctionprogressHandler(event:ProgressEvent):void{  
  30. 30lbProgress.text="已上传"+(event.bytesLoaded/1024).toFixed(2)+"K,共"+(event.bytesTotal/1024).toFixed(2)+"K";  
  31. 31varproc:uint=event.bytesLoaded/event.bytesTotal*100;  
  32. 32progress1.setProgress(proc,100);  
  33. 33progress1.label="当前进度:"+""+proc+"%";  
  34. 34  
  35. 35}  
  36. 36privatefunctionselectHandler(event:Event):void{  
  37. 37varrequest:URLRequest=newURLRequest("http://localhost:9080/upload/upload.jsp")  
  38. 38try  
  39. 39{  
  40. 40fileRef.upload(request);  
  41. 41}  
  42. 42catch(error:Error)  
  43. 43{  
  44. 44trace("Unabletouploadfile."+error.toString());  
  45. 45}  
  46. 46}  
  47. 47privatefunctioncompleteHandler(event:Event):void{  
  48. 48trace("uploaded");  
  49. 49}  

Flex上传文件效果图:


【编辑推荐】

  1. 实例解析Flex字体的使用
  2. FlexBuilder4十大新特性闪亮登场
  3. 学习总结 在Flex中如何嵌入Flex字体
  4. 揭开Flex正则表达式的神秘面纱
  5. FlexBuilder开发方法及特点解析 

 

责任编辑:佚名 来源: cnblogs.com
相关推荐

2010-08-06 13:22:48

FlexCSS

2024-03-27 08:28:31

元素拖拽API文件上传

2010-07-28 08:44:12

Flex2.0

2010-07-27 10:39:25

Flex组件

2010-08-10 16:41:54

FlexJSP

2010-08-04 09:26:27

Flex数据

2010-08-10 09:40:23

Flex与浏览器交互

2010-08-12 11:05:33

Flex数据绑定

2010-07-20 15:26:26

Perl文件

2009-11-16 14:15:51

PHP上传多个文件

2010-07-30 13:52:17

Flex组件

2010-08-10 15:26:38

Flex应用程序

2010-07-30 13:08:38

Flex调用JavaS

2010-08-09 10:34:05

Flex背景

2009-11-16 13:04:04

PHP上传文件代码

2010-08-05 15:46:13

Flex行为Flex效果

2010-07-30 09:28:09

Flex数据绑定

2010-08-03 14:52:49

Flex界面设计

2009-11-16 11:41:19

PHP上传大文件

2010-07-27 13:46:18

Flex swf
点赞
收藏

51CTO技术栈公众号