FLASHCS3多文件上传源代码(类似uccenter社区)

本文介绍如何使用FLASH CS3与ActionScript 3.0实现多文件上传功能,包括上传进度显示及文件筛选等功能。通过具体代码示例,帮助开发者理解整个上传流程。

最近项目中用到了FLASH多文件上传功能,根据虽然CS4是最新的,但还是选择了CS3

FLASH CS3,使用的是ActionScript 3.0,在开发的过程中发现 AS3与AS2的区别是非常大的,比如geturl等,下面是完整源代码,希望对大家有用:

 

上传进度控件cs文件

 

  1. package
  2. {
  3.     import flash.display.*;
  4.     import flash.text.*;
  5.     
  6.     public class UCProgress extends MovieClip
  7.     {
  8.         public function UCProgress()
  9.         {
  10.             _txt.autoSize = TextFieldAutoSize.LEFT;
  11.             _txt.text = '已完成:0%';
  12.             _mc.width = 0;
  13.         }
  14.         public function ready(n):void
  15.         {
  16.             _txt.text = '就绪:' + n;
  17. //          _txt.textColor = 0x666666;
  18.         }
  19.         public function updateinfo(n,b,t,fn):void
  20.         {
  21.             _txt.text = fn
  22.             _txt1.text = n + '%';
  23. //          _txt.textColor = 0xff6600;
  24.             _mc.width = b/t * 165;
  25.         }
  26.         public function complete(n):void
  27.         {
  28.             _txt.text = '上传完成:' + n;
  29. //          _txt.textColor = 0x0066cc;
  30.         }
  31.     }
  32. }

主cs文件

  1. package {
  2.     //kervin:Flash文件上传类
  3.     import flash.net.FileReference;
  4.     import flash.net.FileReferenceList;
  5.     import fl.data.DataProvider;
  6.     import fl.controls.List;
  7.     import fl.controls.ProgressBar;
  8.     import fl.controls.ProgressBarMode;
  9.     import fl.controls.*;
  10.     import flash.display.Sprite;
  11.     import flash.events.*;
  12.     import flash.display.MovieClip;
  13.     import flash.net.*;
  14.     //load url parameters
  15.     import flash.display.LoaderInfo;
  16.     //javascript
  17.     import flash.external.ExternalInterface;
  18.     public class a8uploader extends MovieClip {
  19.         
  20.         //define a url for upload
  21.         var uploadURL = "http://www.xxx.com/a8uploader.php?albumid=";
  22.         var total:Number = 0;
  23.         var albumId:Number = 0;
  24.         var albumList:String = "";
  25.         var arr:Array=new Array();
  26.         var arrlistpro:Array = new Array();
  27.         var dp:DataProvider = new DataProvider();
  28.         var fileRef = new FileReference();
  29.         var currentIndex:Number = 0;
  30.         //define upload type of file
  31.         var images_filter:FileFilter = new FileFilter("*.jpg, *.gif""*.jpg;*.gif");
  32.         var docs_filter:FileFilter = new FileFilter("*.txt, *.doc, *.docx, *.rtf""*.txt;*.doc;*.docx;*.rtf");
  33.         var zip_filter:FileFilter = new FileFilter("*.zip, *.rar""*.zip;*.rar");
  34.         var dataPro:DataProvider;
  35.         var completed:Boolean = true;
  36.         //construct function
  37.         function a8uploader() {
  38.             var loadParms:Array = new Array();
  39.             this.labeltest.text = '就绪';
  40.             var keyStr:String;
  41.             var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
  42.             for (keyStr in paramObj) {
  43.                 loadParms[keyStr] = paramObj[keyStr].toString();
  44.                 //this.labeltest.text += (keyStr+":"+loadParms[keyStr]+"-");
  45.                 if (keyStr == "id") {
  46.                     this.albumId = loadParms[keyStr];
  47.                 } else {
  48.                     this.albumList = loadParms[keyStr];
  49.                 }
  50.             }
  51.             bt_add.addEventListener(MouseEvent.CLICK,toupload);
  52.             bt_upload.addEventListener(MouseEvent.CLICK,topost);
  53.             bt_del.addEventListener(MouseEvent.CLICK,todel);
  54.             /*var loader:URLLoader = URLLoader(event.target);
  55.             var vars:URLVariables = new URLVariables(loader.data);*/
  56.             var tempList:Array = this.albumList.split("||");
  57.             var tempName:String = "";
  58.             var tempValue:String = "";
  59.             var tempArr:Array = new Array();
  60.             //combobox binding...
  61.             for (var i:uint = 0; i < tempList.length; i++) {
  62.                 tempArr = tempList[i].split("^");
  63.                 this.cb_album.addItem({label:tempArr[0], val:tempArr[1]});
  64.                 if (tempArr[1] == this.albumId) {
  65.                     this.cb_album.selectedIndex = i;
  66.                 }
  67.             }
  68.             cb_album.labelField = "label";
  69.             //loader.addEventListener(Event.OPEN, openHandler);
  70.         }
  71.         /*private function loaderCompleteHandler(event:Event):void {
  72.         var loader:URLLoader = URLLoader(event.target);
  73.         var vars:URLVariables = new URLVariables(loader.data);
  74.         this.albumId = vars.id;
  75.         this.albumList = vars.liststr;
  76.         
  77.         var tempList:Array = this.albumList.split("||");
  78.         var tempName:String = "";
  79.         var tempValue:String = "";
  80.         var tempArr:Array = new Array();
  81.         this.labeltest.text = this.albumList;
  82.         for (var i:uint = 0; i < tempList.length; i++) {
  83.         tempArr = tempList.splice("^");
  84.         this.cb_album.addItem({label:tempArr[0], val:tempArr[1]});
  85.         if(tempArr[1] == this.albumId){
  86.         this.cb_album.selectedIndex = i;
  87.         }
  88.         }
  89.         cb_album.labelField = "label";
  90.         }*/
  91.         /*private function openHandler(event:Event):void {
  92.                trace("openHandler: " + event);
  93.           }*/
  94.           
  95.         //delete a item
  96.         function todel(event:MouseEvent):void {
  97.             if (this.completed) {
  98.                 var l = this.lst_files.length;
  99.                 for (var j = 0; j<l; j++) {
  100.                     if (this.lst_files.getItemAt(j).label == this.lst_files.selectedItem.label) {
  101.                         //_list.getItemAt(j).data == "test";
  102.                         this.lst_files.removeItemAt(j);
  103.                         arrlistpro.splice(j,1);
  104.                         arr.splice(j,1);
  105.                         break;
  106.                     }
  107.                 }
  108.             } else {
  109.                 this.labeltest.text = '上传中...';
  110.             }
  111.         }
  112.         //clear all item
  113.         function clearall():void {
  114.             if (this.completed) {
  115.                 this.lst_files.removeAll();
  116.                 this.arrlistpro = new Array();
  117.                 this.arr = new Array();
  118.                 this.dp.removeAll();
  119.             } else {
  120.                 this.labeltest.text = '上传中...';
  121.             }
  122.         }
  123.         //load progress class
  124.         function progressHandler(event:ProgressEvent):void {
  125.             var file:FileReference = FileReference(event.target);
  126.             try {
  127.                 var list:Array = new Array();
  128.                 var curlist = arrlistpro[currentIndex].source;
  129.                 curlist.updateinfo(Math.round((event.bytesLoaded / event.bytesTotal) * 100),event.bytesLoaded,event.bytesTotal,file.name);
  130.                 dataPro = new DataProvider(arrlistpro);
  131.                 this.lst_files.dataProvider = dataPro;
  132.             } catch (error:Error) {
  133.                 trace(error);
  134.             }
  135.         }
  136.         //exception handler
  137.         function ioErrorHandler(event:IOErrorEvent):void {
  138.             this.labeltest.text = '传输过程发生错误...';
  139.         }
  140.         //uploaded
  141.         function completeHandler(event:Event):void {
  142.             if (this.currentIndex < this.total-1) {
  143.                 this.currentIndex++;
  144.                 tosuccess();
  145. //              this.labeltest.text = "currentIndex:"+this.currentIndex.toString()+"/"+this.total.toString();
  146.             } else {
  147.                 this.labeltest.text = (this.currentIndex + 1) + '个文件已上传';
  148.                 this.completed = true;
  149.                 this.currentIndex = 0;
  150.                 clearall();
  151.                 ref_url();
  152.             }
  153.         }
  154.         //call javascript
  155.         function ref_url():void{
  156.            ExternalInterface.call("goheadupload",this.albumId);
  157.         }
  158.         //begin upload
  159.         function topost(event:MouseEvent):void {
  160.             //this.labeltest.text = '开始上传...';
  161.             if (this.completed) {
  162.                 this.completed = false;
  163.                 if (arr != null && arr.length >0) {
  164.                     this.total = arr.length;
  165.                     dataPro = new DataProvider(arrlistpro);
  166.                     this.lst_files.dataProvider = dataPro;
  167.                     tosuccess();
  168.                 } else {
  169.                     this.labeltest.text = '请先选择上传的文件!';
  170.                 }
  171.             } else {
  172.                 this.labeltest.text = '上传中...';
  173.             }
  174.         }
  175.         //upload function
  176.         function tosuccess() {
  177.             var file:FileReference = arr[currentIndex] as FileReference;
  178.             var fullUrl:String = "";
  179.             var item:Object = this.cb_album.selectedItem;
  180.             if (this.cb_album.selectedIndex > -1) {
  181.                 this.albumId = item.val;
  182.             }
  183.             if (this.albumId > 0) {
  184.                 fullUrl = uploadURL + this.albumId + "&size=" + getSize(file.size);
  185.                 file.upload(new URLRequest(fullUrl));
  186.             }
  187.         }
  188.         //get file size
  189.         function getSize(size:Number):String {
  190.             var tmp = Math.ceil(size / 1024);
  191.             return tmp.toString() + "k";
  192.         }
  193.         //select for upload
  194.         function toupload(event:MouseEvent):void {
  195.             if (this.completed) {
  196.                 fileRef = new FileReferenceList();
  197.                 fileRef.addEventListener(Event.SELECT, selectHandler);
  198.                 fileRef.browse([images_filter, docs_filter, zip_filter]);
  199.             } else {
  200.                 this.labeltest.text = '上传中...';
  201.             }
  202.         }
  203.         //load to fileList
  204.         function selectHandler(event:Event):void {
  205.             var fileRefList:FileReferenceList = FileReferenceList(event.target);
  206.             var fileList:Array = fileRefList.fileList;
  207.             var file:FileReference;
  208.             var pro:UCProgress;
  209.             for (var i:uint = 0; i < fileList.length; i++) {
  210.                 file = fileList[i];
  211.                 var fsize = Math.ceil(file.size / 1024);
  212.                 if(fsize > 2048){
  213.                     this.labeltest.text = "文件尺寸过大,请选择2M以下的图片";
  214.                 }
  215.                 file.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  216.                 file.addEventListener(Event.COMPLETE,completeHandler);
  217.                 pro = new UCProgress;
  218.                 arrlistpro.push({source:pro});
  219.                 dp.addItem( { label:(file.name + " (" + getSize(file.size) + ")")});
  220.                 arr.push(file);
  221.             }
  222.             this.lst_files.dataProvider = dp;
  223.         }
  224.     }
  225. }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值