Flex中使用FileReference类多文件上传

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  3.    layout="vertical" horizontalAlign="left" fontSize="12"  
  4.     initialize="init()"  
  5.     viewSourceURL="srcview/index.html">  
  6.    <mx:NumberFormatter id="filesizeFormatter" useThousandsSeparator="true"/>  
  7.    <mx:Script>  
  8.         <![CDATA[ 
  9.            import mx.events.CollectionEvent; 
  10.            import mx.formatters.NumberFormatter; 
  11.            import mx.formatters.CurrencyFormatter; 
  12.           import mx.collections.ArrayCollection; 
  13.             import mx.controls.Alert; 
  14.            private var fileRefs: FileReferenceList = new FileReferenceList(); 
  15.            //这个地址是我测试用的服务器地址 
  16.            private var urlrequest: URLRequest = new URLRequest("http://localhost:8080/abc/UploadFile"); 
  17.           [Bindable] 
  18.            private var selectedFiles: ArrayCollection = new ArrayCollection([]); 
  19.             private var singleThreadFiles: Array = []; 
  20.             [Bindable] 
  21.             private var useSingleThread: Boolean = true; 
  22.            private function init(): void 
  23.            { 
  24.                Security.allowDomain("*"); 
  25.                fileRefs.addEventListener(Event.SELECT, fileSelectHandler); 
  26.               fileRefs.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
  27.               fileRefs.addEventListener(Event.COMPLETE, completeHandler); 
  28.               addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);    
  29.            } 
  30.            private function selectFile(): void 
  31.            { 
  32.               fileRefs.browse([new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png"), 
  33.                                       new FileFilter("所有文件(*.*)", "*.*") 
  34.                            ]); 
  35.            } 
  36.           private function fileSelectHandler(event: Event): void 
  37.            { 
  38.                for each (var f: FileReference in fileRefs.fileList) 
  39.                { 
  40.                    selectedFiles.addItem(f); 
  41.                } 
  42.             } 
  43.             private function uploadFile(): void 
  44.             {                
  45.                for each (var f: FileReference in selectedFiles) 
  46.                 {            
  47.                    try 
  48.                    { 
  49.                       f.upload(urlrequest); 
  50.                    } 
  51.                    catch (e: Error) 
  52.                    { 
  53.                        Alert.show(e.message); 
  54.                     } 
  55.                 }                
  56.            } 
  57.            private function singleThreadUploadFile(): void 
  58.            { 
  59.                //FIFO:逐个从列表中取出,进行同步上传 
  60.                if (singleThreadFiles.length > 0) 
  61.                { 
  62.                    var f: FileReference = singleThreadFiles.pop() as FileReference; 
  63.                    f.addEventListener(Event.COMPLETE, doSingleUploadFileComplete); 
  64.                    f.upload(urlrequest); 
  65.                 } 
  66.           } 
  67.             private function doSingleUploadFileComplete(event: Event): void 
  68.            { 
  69.                var f: FileReference = event.target as FileReference; 
  70.                f.removeEventListener(Event.COMPLETE, doSingleUploadFileComplete); 
  71.                singleThreadUploadFile(); 
  72.            } 
  73.           private function ioErrorHandler(e:IOErrorEvent): void 
  74.            { 
  75.               Alert.show(e.text); 
  76.            } 
  77.            private function completeHandler(e: Event): void 
  78.             { 
  79.                 img.source = e.target.data; 
  80.             } 
  81.             private function showImage(e: Event): void 
  82.             {                
  83.                 var f: FileReference = (e.target as DataGrid).selectedItem as FileReference; 
  84.                 f.addEventListener(Event.COMPLETE, completeHandler); 
  85.                 //f.load(); 
  86.             
  87.             } 
  88.             public function removeFile(f: FileReference): void 
  89.             { 
  90.                 var index: int = selectedFiles.getItemIndex(f); 
  91.                 if (index != -1) 
  92.                     selectedFiles.removeItemAt(index); 
  93.             } 
  94.         ]]>  
  95.     </mx:Script>  
  96.     <mx:VBox>  
  97.         <mx:HBox width="100%">  
  98.             <mx:Button id="selectFileButton" label="浏览" click="selectFile()"/>  
  99.             <mx:Box width="100%" horizontalAlign="right">  
  100.                 <mx:Button click="selectedFiles.removeAll();" label="清空"/>  
  101.             </mx:Box>  
  102.         </mx:HBox>  
  103.         <mx:DataGrid id="files" dataProvider="{selectedFiles}" change="showImage(event)">  
  104.             <mx:columns>  
  105.                 <mx:DataGridColumn width="150" headerText="文件名" dataField="name" />  
  106.                 <mx:DataGridColumn headerText="大小(字节)" dataField="size">  
  107.                     <mx:itemRenderer>  
  108.                         <mx:Component>  
  109.                             <mx:Label text="{outerDocument.filesizeFormatter.format(data.size)}" textAlign="right"/>  
  110.                         </mx:Component>  
  111.                     </mx:itemRenderer>  
  112.                 </mx:DataGridColumn>  
  113.                 <mx:DataGridColumn headerText="上传进度" width="300">  
  114.                     <mx:itemRenderer>  
  115.                         <mx:Component>  
  116.                             <mx:HBox fontSize="10" fontWeight="normal" fontThickness="1">  
  117.                                 <mx:Script>  
  118.                                     <![CDATA[ 
  119.                                         import flash.profiler.showRedrawRegions; 
  120.                                         import mx.controls.Alert; 
  121.                                          import mx.controls.ProgressBar; 
  122.                                          private function initProgressBar(event: Event): void 
  123.                                          { 
  124.                                             //使progressbar与file关联,从而产生进度条 
  125.                                             var pb: ProgressBar = event.target as ProgressBar; 
  126.                                             pb.label = "%3%%"; 
  127.                                              pb.setProgress(0, 100); 
  128.                                              var f: FileReference = data as FileReference; 
  129.                                              //使用闭包方法,更新进度条 
  130.                                              f.addEventListener(ProgressEvent.PROGRESS, 
  131.                                                  function(event: ProgressEvent): void 
  132.                                                  { 
  133.                                                      pb.setProgress(event.bytesLoaded, event.bytesTotal); 
  134.                                                  } 
  135.                                             ); 
  136.                                             f.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, 
  137.                                                 function (event: DataEvent): void 
  138.                                                 { 
  139.                                                     //服务器端一定要返回数据,否则,这个方法就不起作用了 
  140.                                                     pb.label = event.data; 
  141.                                                 } 
  142.                                             );                                            
  143.                                          } 
  144.                                     ]]>  
  145.                                 </mx:Script>  
  146.                                 <mx:ProgressBar verticalCenter="true" width="100%" paddingLeft="5" paddingRight="5"  
  147.                                     maximum="100" minimum="0" labelPlacement="center" mode="manual"  
  148.                                     label="%3%%" textAlign="left"  
  149.                                     creationComplete="initProgressBar(event)"/>  
  150.                                 <mx:LinkButton label="Cancel">  
  151.                                     <mx:click>  
  152.                                         <![CDATA[ 
  153.                                             var f: FileReference = data as FileReference; 
  154.                                             f.cancel(); 
  155.                                         ]]>  
  156.                                     </mx:click>  
  157.                                 </mx:LinkButton>  
  158.                                 <mx:LinkButton label="Delete">  
  159.                                     <mx:click>  
  160.                                         <![CDATA[ 
  161.                                             var f: FileReference = data as FileReference; 
  162.                                             outerDocument.removeFile(f); 
  163.                                         ]]>  
  164.                                     </mx:click>  
  165.                                 </mx:LinkButton>  
  166.                             </mx:HBox>  
  167.                         </mx:Component>  
  168.                     </mx:itemRenderer>  
  169.                 </mx:DataGridColumn>  
  170.             </mx:columns>  
  171.         </mx:DataGrid>  
  172.     </mx:VBox>  
  173.     <mx:HBox>  
  174.         <mx:Button label="上传">  
  175.             <mx:click>  
  176.                 <![CDATA[ 
  177.                     if (useSingleThread) 
  178.                     { 
  179.                         //逐个上传 
  180.                        singleThreadFiles = selectedFiles.toArray().concat(); 
  181.                         singleThreadFiles.reverse(); 
  182.                         singleThreadUploadFile(); 
  183.                     } 
  184.                     else 
  185.                     { 
  186.                         //多个文件同时上传 
  187.                         uploadFile(); 
  188.                     } 
  189.                 ]]>  
  190.             </mx:click>  
  191.         </mx:Button>  
  192.         <mx:CheckBox id="checkboxSingleThread" label="同时上传多个文件" selected="{!useSingleThread}"  
  193.             change="useSingleThread = !checkboxSingleThread.selected"/>  
  194.     </mx:HBox>  
  195.     <mx:Image id="img" width="400" height="300"/>  
  196. </mx:Application> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值