學(xué)習(xí)筆記 剖析Flex上傳文件功能如何實(shí)現(xiàn)
本文和大家重點(diǎn)討論一下Flex上傳文件功能如何實(shí)現(xiàn),寫(xiě)過(guò)很多文件上傳的功能,包括AJAX實(shí)現(xiàn)動(dòng)態(tài)監(jiān)控上傳進(jìn)度的,現(xiàn)在看到了實(shí)現(xiàn)Flex文件上傳功能,還真是很方便,這里和大家分享一下。
Flex上傳文件功能
寫(xiě)過(guò)很多文件上傳的功能,包括AJAX實(shí)現(xiàn)動(dòng)態(tài)監(jiān)控上傳進(jìn)度的,現(xiàn)在看到了實(shí)現(xiàn)Flex文件上傳功能,還真是很方便,沒(méi)什么好說(shuō)的,F(xiàn)lex上傳文件代碼:
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="當(dāng)前進(jìn)度:0%"styleName="myfont"fontWeight="normal"/>
- <mx:Labelxmx:Labelx="146"y="98"width="321"id="lbProgress"styleName="myfont"textAlign="right"/>
- </mx:Application>
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="當(dāng)前進(jìn)度:"+""+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}
Flex上傳文件效果圖:
【編輯推薦】
- 實(shí)例解析Flex字體的使用
- FlexBuilder4十大新特性閃亮登場(chǎng)
- 學(xué)習(xí)總結(jié) 在Flex中如何嵌入Flex字體
- 揭開(kāi)Flex正則表達(dá)式的神秘面紗
- FlexBuilder開(kāi)發(fā)方法及特點(diǎn)解析