最近專案中用到了FLASH多檔案上傳功能,根據雖然CS4是最新的,但還是選擇了CS3
FLASH CS3,使用的是ActionScript 3.0,在開發的過程中發現 AS3與AS2的區別是非常大的,比如geturl等,下面是完整原始碼,希望對大家有用:
 
上傳進度控制元件cs檔案
 
package 

    import flash.display.*; 
    import flash.text.*; 
     
    public class UCProgress extends MovieClip 
    { 
        public function UCProgress() 
        { 
            _txt.autoSize = TextFieldAutoSize.LEFT; 
            _txt.text = '已完成:0%'; 
            _mc.width = 0; 
        } 
        public function ready(n):void 
        { 
            _txt.text = '就緒:' + n; 
//          _txt.textColor = 0x666666; 
        } 
        public function updateinfo(n,b,t,fn):void 
        { 
            _txt.text = fn 
            _txt1.text = n + '%'; 
//          _txt.textColor = 0xff6600; 
            _mc.width = b/t * 165; 
        } 
        public function complete(n):void 
        { 
            _txt.text = '上傳完成:' + n; 
//          _txt.textColor = 0x0066cc; 
        }
    } 
}
主cs檔案
package { 
    //kervin:Flash檔案上傳類 
    import flash.net.FileReference; 
    import flash.net.FileReferenceList; 
    import fl.data.DataProvider; 
    import fl.controls.List; 
    import fl.controls.ProgressBar; 
    import fl.controls.ProgressBarMode; 
    import fl.controls.*; 
    import flash.display.Sprite; 
    import flash.events.*; 
    import flash.display.MovieClip; 
    import flash.net.*; 
    //load url parameters 
    import flash.display.LoaderInfo; 
    //javascript 
    import flash.external.ExternalInterface;
    public class a8uploader extends MovieClip { 
         
        //define a url for upload 
        var uploadURL = "http://www.xxx.com/a8uploader.php?albumid=";
        var total:Number = 0; 
        var albumId:Number = 0; 
        var albumList:String = "";
        var arr:Array=new Array(); 
        var arrlistpro:Array = new Array();
        var dp:DataProvider = new DataProvider(); 
        var fileRef = new FileReference();
        var currentIndex:Number = 0;
        //define upload type of file 
        var images_filter:FileFilter = new FileFilter("*.jpg, *.gif", "*.jpg;*.gif"); 
        var docs_filter:FileFilter = new FileFilter("*.txt, *.doc, *.docx, *.rtf", "*.txt;*.doc;*.docx;*.rtf"); 
        var zip_filter:FileFilter = new FileFilter("*.zip, *.rar", "*.zip;*.rar"); 
        var dataPro:DataProvider;
        var completed:Boolean = true;
        //construct function 
        function a8uploader() { 
            var loadParms:Array = new Array(); 
            this.labeltest.text = '就緒'; 
            var keyStr:String; 
            var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; 
            for (keyStr in paramObj) { 
                loadParms[keyStr] = paramObj[keyStr].toString(); 
                //this.labeltest.text += (keyStr+":"+loadParms[keyStr]+"-"); 
                if (keyStr == "id") { 
                    this.albumId = loadParms[keyStr]; 
                } else { 
                    this.albumList = loadParms[keyStr]; 
                } 
            } 
            bt_add.addEventListener(MouseEvent.CLICK,toupload); 
            bt_upload.addEventListener(MouseEvent.CLICK,topost); 
            bt_del.addEventListener(MouseEvent.CLICK,todel);
            /*var loader:URLLoader = URLLoader(event.target); 
            var vars:URLVariables = new URLVariables(loader.data);*/ 
            var tempList:Array = this.albumList.split("||"); 
            var tempName:String = ""; 
            var tempValue:String = ""; 
            var tempArr:Array = new Array();
            //combobox binding... 
            for (var i:uint = 0; i < tempList.length; i++) { 
                tempArr = tempList[i].split("^"); 
                this.cb_album.addItem({label:tempArr[0], val:tempArr[1]}); 
                if (tempArr[1] == this.albumId) { 
                    this.cb_album.selectedIndex = i; 
                } 
            } 
            cb_album.labelField = "label"; 
            //loader.addEventListener(Event.OPEN, openHandler); 
        } 
        /*private function loaderCompleteHandler(event:Event):void { 
        var loader:URLLoader = URLLoader(event.target); 
        var vars:URLVariables = new URLVariables(loader.data); 
        this.albumId = vars.id; 
        this.albumList = vars.liststr; 
         
        var tempList:Array = this.albumList.split("||"); 
        var tempName:String = ""; 
        var tempValue:String = ""; 
        var tempArr:Array = new Array(); 
        this.labeltest.text = this.albumList; 
        for (var i:uint = 0; i < tempList.length; i++) { 
        tempArr = tempList.splice("^"); 
        this.cb_album.addItem({label:tempArr[0], val:tempArr[1]}); 
        if(tempArr[1] == this.albumId){ 
        this.cb_album.selectedIndex = i; 
        } 
        } 
        cb_album.labelField = "label"; 
        }*/ 
        /*private function openHandler(event:Event):void { 
               trace("openHandler: " + event); 
          }*/ 
           
        //delete a item 
        function todel(event:MouseEvent):void { 
            if (this.completed) { 
                var l = this.lst_files.length; 
                for (var j = 0; j<l; j++) { 
                    if (this.lst_files.getItemAt(j).label == this.lst_files.selectedItem.label) { 
                        //_list.getItemAt(j).data == "test"; 
                        this.lst_files.removeItemAt(j); 
                        arrlistpro.splice(j,1); 
                        arr.splice(j,1); 
                        break; 
                    } 
                } 
            } else { 
                this.labeltest.text = '上傳中...'; 
            } 
        } 
        //clear all item 
        function clearall():void { 
            if (this.completed) { 
                this.lst_files.removeAll(); 
                this.arrlistpro = new Array(); 
                this.arr = new Array(); 
                this.dp.removeAll(); 
            } else { 
                this.labeltest.text = '上傳中...'; 
            } 
        } 
        //load progress class 
        function progressHandler(event:ProgressEvent):void { 
            var file:FileReference = FileReference(event.target); 
            try { 
                var list:Array = new Array(); 
                var curlist = arrlistpro[currentIndex].source; 
                curlist.updateinfo(Math.round((event.bytesLoaded / event.bytesTotal) * 100),event.bytesLoaded,event.bytesTotal,file.name); 
                dataPro = new DataProvider(arrlistpro); 
                this.lst_files.dataProvider = dataPro; 
            } catch (error:Error) { 
                trace(error); 
            } 
        } 
        //exception handler 
        function ioErrorHandler(event:IOErrorEvent):void { 
            this.labeltest.text = '傳輸過程發生錯誤...'; 
        } 
        //uploaded 
        function completeHandler(event:Event):void { 
            if (this.currentIndex < this.total-1) { 
                this.currentIndex++; 
                tosuccess(); 
//              this.labeltest.text = "currentIndex:"+this.currentIndex.toString()+"/"+this.total.toString(); 
            } else { 
                this.labeltest.text = (this.currentIndex + 1) + '個檔案已上傳'; 
                this.completed = true; 
                this.currentIndex = 0; 
                clearall(); 
                ref_url(); 
            } 
        } 
        //call javascript 
        function ref_url():void{ 
           ExternalInterface.call("goheadupload",this.albumId); 
        } 
        //begin upload 
        function topost(event:MouseEvent):void { 
            //this.labeltest.text = '開始上傳...'; 
            if (this.completed) { 
                this.completed = false; 
                if (arr != null && arr.length >0) { 
                    this.total = arr.length; 
                    dataPro = new DataProvider(arrlistpro); 
                    this.lst_files.dataProvider = dataPro; 
                    tosuccess(); 
                } else { 
                    this.labeltest.text = '請先選擇上傳的檔案!'; 
                } 
            } else { 
                this.labeltest.text = '上傳中...'; 
            } 
        } 
        //upload function 
        function tosuccess() { 
            var file:FileReference = arr[currentIndex] as FileReference; 
            var fullUrl:String = ""; 
            var item:Object = this.cb_album.selectedItem; 
            if (this.cb_album.selectedIndex > -1) { 
                this.albumId = item.val; 
            } 
            if (this.albumId > 0) { 
                fullUrl = uploadURL + this.albumId + "&size=" + getSize(file.size); 
                file.upload(new URLRequest(fullUrl)); 
            } 
        } 
        //get file size 
        function getSize(size:Number):String { 
            var tmp = Math.ceil(size / 1024); 
            return tmp.toString() + "k"; 
        } 
        //select for upload 
        function toupload(event:MouseEvent):void { 
            if (this.completed) { 
                fileRef = new FileReferenceList(); 
                fileRef.addEventListener(Event.SELECT, selectHandler); 
                fileRef.browse([images_filter, docs_filter, zip_filter]); 
            } else { 
                this.labeltest.text = '上傳中...'; 
            } 
        } 
        //load to fileList 
        function selectHandler(event:Event):void { 
            var fileRefList:FileReferenceList = FileReferenceList(event.target); 
            var fileList:Array = fileRefList.fileList; 
            var file:FileReference; 
            var pro:UCProgress; 
            for (var i:uint = 0; i < fileList.length; i++) { 
                file = fileList[i]; 
                var fsize = Math.ceil(file.size / 1024); 
                if(fsize > 2048){ 
                    this.labeltest.text = "檔案尺寸過大,請選擇2M以下的圖片"; 
                } 
                file.addEventListener(ProgressEvent.PROGRESS,progressHandler); 
                file.addEventListener(Event.COMPLETE,completeHandler); 
                pro = new UCProgress; 
                arrlistpro.push({source:pro}); 
                dp.addItem( { label:(file.name + " (" + getSize(file.size) + ")")}); 
                arr.push(file); 
            } 
            this.lst_files.dataProvider = dp; 
        } 
    } 
}