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

最近项目中用到了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;
        }
    }
}

 

本文出自 “WPF外包公司―北京动点..” 博客,转载请与作者联系!

你可能感兴趣的:(源代码,文件,社区,FLASHCS,uccenter)