• home > webfront > ECMAS > AS >

    flash上传文件的actionscript代码

    Author:[email protected] Date:

    flash上传文件的actionscript代码
    import flash.net.FileReference;
        import flash.net.URLRequest;
        import flash.net.FileFilter;
        import flash.display.Sprite;
        import flash.events.*;
    
    
    
        var fileRef = new FileReference();
        var fileListener = new Object();
        var totalBytes:Number = 0;
        var uploadedBytes:Number = 0;
    
        var acceptFileType:String = getFlashVars("acceptFileType");
        var comJsFun:String=getFlashVars("ComJsFun");
        //this.tbFilePath.text = comJsFun;
    
        var uploadURL:URLRequest = new URLRequest();
        var xmlRequest:URLRequest = new URLRequest();
        var xmlLoader:URLLoader = new URLLoader();
    
        //初始化系统
        function init() {
            this.mcWaterStyle.visible = false;
            this.mcFilePlayer.visible = false;
            this.progressBar.visible = false;
    
    //设置按钮文字大小。
            var myFormat = new TextFormat();
            myFormat.size = 12;
            this.btBrowser.setStyle("textFormat", myFormat);
            this.btUpload.setStyle("textFormat",myFormat);
            this.tbFilePath.setStyle("textFormat", myFormat);
    
            this.mcWaterStyle.style0.setStyle("textFormat", myFormat);
            this.mcWaterStyle.style1.setStyle("textFormat", myFormat);
            this.mcWaterStyle.style2.setStyle("textFormat", myFormat);
            this.uploadInfo.setStyle("textFormat", myFormat);
    
            if(acceptFileType==""){
                /*从外部获取上传附加的文件类型。*/
                xmlRequest = new URLRequest("swffileupload.xml");
    //var loadVidateFile:URLRequest = new URLRequest("http://192.168.18.4/admin/article/x.aspx");
                xmlLoader.load(xmlRequest);
                xmlLoader.addEventListener(Event.COMPLETE,loaderHandler);
            }
        }
    
        init();
    
        //按钮事件
        this.btBrowser.addEventListener(MouseEvent.CLICK, browseHandler);
        this.btUpload.addEventListener(MouseEvent.CLICK, UploadHandler);
    
        fileRef.addEventListener(Event.SELECT,selectHandler);
        fileRef.addEventListener(Event.CANCEL, cancelHandler);
        fileRef.addEventListener(Event.OPEN, openHandler);
        fileRef.addEventListener(Event.COMPLETE, completeHandler);
        fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler)
        fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    
        fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    
    
        //浏览按钮事件
        function browseHandler(event:MouseEvent):void {
            fileRef.browse(browseGetTypes());
        }
        function browseGetTypes():Array {
            var allTypes:Array = new Array(browseGetFileTypeFilter());
            return allTypes;
        }
    
        function browseGetFileTypeFilter():FileFilter {
    
            if(acceptFileType!=""){
                var arr = acceptFileType.split("|");
                var FileExtension = "";
                for(var i=0;i<arr.length;i++){
                    if (arr[i] == "")
                        continue;
                    if(i==0)
                        FileExtension = "*."+arr[i];
                    else
                        FileExtension = FileExtension+";*."+arr[i];
                }
                return new FileFilter("文件格式("+FileExtension+")", ""+FileExtension+"");
            }else{
                return new FileFilter("所有文件(*.*)","*.*");
            }
    
    //return new FileFilter("所有文件(*.*)","*.*");
        }
    
        //加载上传配置事件
        function loaderHandler(event:Event):void {
            var myXML:XML = new XML(xmlLoader.data);
        //myXML = XML();
        acceptFileType = myXML.child("Upload").ContontFileStyle;
        trace("--"+myXML.child("Upload").ContontFileStyle+"--");
        if(this.uploadInfo.text=="")
            this.uploadInfo.text = "允许的格式:"+myXML.child("Upload").ContontFileStyle;
        }
    
        //选择文件事件
        function selectHandler(event:Event):void{
            this.uploadInfo.text = "";
        this.mcWaterStyle.visible = false;
        this.mcFilePlayer.visible = false;
    
        if (fileRef.size > 0){
            totalBytes = fileRef.size;
            this.tbFilePath.text =  fileRef.name+ "[" + this.getSizeType(totalBytes) + "]";
            var fileExtName = fileRef.name.substring(fileRef.name.lastIndexOf(".")+1);
            switch(fileExtName.toLowerCase()){
                case "jpg":
                case "png":
                case "jpeg":
                case "gif":
                case "bmp":
                    this.mcWaterStyle.visible = true;
                    break;
                case "flv":
                    this.mcFilePlayer.visible = true;
                    this.mcFilePlayer.mcWidth.text = 410;
                    this.mcFilePlayer.mcHeight.text = 370;
                    break;
                case "swf":
                    this.mcFilePlayer.visible = true;
                    this.mcFilePlayer.mcWidth.text = 550;
                    this.mcFilePlayer.mcHeight.text = 400;
                    break;
                case "rm":
                case "rmvb":
                case "mp3":
                case "avi":
                case "mpg":
                case "mpeg":
                case "asf":
                case "wmv":
                case "wma":
                    this.mcFilePlayer.visible = true;
                    this.mcFilePlayer.mcWidth.text = 550;
                    this.mcFilePlayer.mcHeight.text = 400;
                    break;
                default:
                    this.mcWaterStyle.visible = false;
                    this.mcFilePlayer.visible = false;
                    break;
            }
        }else{
            this.uploadInfo.text = "错误:您没有选择文件!";
        }
        }
    
        //上传过程中取消
        function cancelHandler(event:Event):void{
            this.progressBar.visible = false;
        };
    
        //当上载或下载操作开始时
        function openHandler(event:Event):void{
            this.tbFilePath.visible = false;
        this.mcWaterStyle.visible = false;
        this.mcFilePlayer.visible = false;
        this.btBrowser.visible = false;
        this.btUpload.label = "取消";
        this.progressBar.visible = true;
        };
    
        //点击伤
        function UploadHandler(event:MouseEvent):void{
            if (this.btUpload.label=="上传"){
            var WaterMarkStyleP:String = "";
            if(this.mcWaterStyle.style0.selected == true)
                WaterMarkStyleP = "0";
            else if(this.mcWaterStyle.style1.selected == true)
                WaterMarkStyleP = "1";
            else if(this.mcWaterStyle.style2.selected == true)
                WaterMarkStyleP = "2";
    
            var mcFilePlayerP:String="&width="+this.mcFilePlayer.mcWidth;
            mcFilePlayerP += "&height="+this.mcFilePlayer.mcHeigth;
    
            if(this.mcFilePlayer.mcAutoYes.selected == true)
                mcFilePlayerP += "&auto=true";
            else if(this.mcFilePlayer.mcAutoNo.selected == true)
                mcFilePlayerP += "&auto=false";
            uploadURL.url="http://localhost:11452/UploadFile.aspx?type=1";
    //uploadURL.url="Upload_File_SWF.aspx?WaterMarkStyle="+WaterMarkStyleP+mcFilePlayerP;
            fileRef.upload(uploadURL);
        }else if(this.btUpload.label=="取消"){
            this.tbFilePath.visible = true;
            this.mcWaterStyle.visible = false;
            this.mcFilePlayer.visible = false;
            this.btBrowser.visible = true;
            this.btUpload.label = "上传";
            this.progressBar.visible = false;
            this.uploadInfo.text="";
        }else if(this.btUpload.label=="重新上传"){
            this.tbFilePath.visible = true;
            this.mcWaterStyle.visible = false;
            this.mcFilePlayer.visible = false;
            this.btBrowser.visible = true;
            this.btUpload.label = "上传";
            this.progressBar.visible = false;
        }
        }
    
        //在文件上载或下载操作期间定期调用
        function progressHandler(event:ProgressEvent):void{
    //var fileRef:FileReference = FileReference(event.target);
            if(event.bytesLoaded==event.bytesTotal){
            this.uploadInfo.text= "正在转移数据,请稍后--"+getSizeType(event.bytesLoaded)+"/"+getSizeType(event.bytesTotal);
        }else{
            this.progressBar.mcMask.width = (event.bytesLoaded/event.bytesTotal)*this.progressBar.mcLoaded.width;
            this.uploadInfo.text="上传文件中,请等待--"+getSizeType(event.bytesLoaded)+"/"+getSizeType(event.bytesTotal);
        }
        }
    
        function completeHandler(event:Event):void {
    //trace("completeHandler: " + event);
            this.tbFilePath.visible = true;
        this.tbFilePath.text = "";
        this.mcWaterStyle.visible = false;
        this.mcFilePlayer.visible = false;
        this.btBrowser.visible = true;
        this.btUpload.label = "上传";
        this.progressBar.visible = false;
        //this.uploadInfo.text = "上传成功!";
        }
    
        function uploadCompleteDataHandler(event:DataEvent):void {
            if(event.data.indexOf("|")!=-1){
            var fileInfoArr = event.data.split("|");
            if(fileInfoArr[0].toLowerCase()=="true"){
                this.uploadInfo.text = "上传成功:"+fileInfoArr[1]+"";
                if(comJsFun!=""){
    //ExternalInterface.call("UploadForEditor","hh");
                    ExternalInterface.call("UploadForEditor",event.data.replace("true|",""));
    //this.tbFilePath.text = event.data;
                }
            }else{
                this.uploadInfo.text = "上传失败:"+EncodeUtf8(fileInfoArr[1])+"";
            }
        }
    
    
    
        trace("uploadCompleteData: " + event.data);
        }
    
    
    
        //错误事件
        function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        this.mcWaterStyle.visible = false;
        this.mcFilePlayer.visible = false;
        this.uploadInfo.text="HTTP错误: " +  event;
        }
        function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        this.mcWaterStyle.visible = false;
        this.mcFilePlayer.visible = false;
        this.uploadInfo.text="IO错误: " +  event;
        }
        function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("openHandler: " + event);
        this.mcWaterStyle.visible = false;
        this.mcFilePlayer.visible = false;
        this.uploadInfo.text="IO安全设置错误: " +  event;
        }
    
    
    
    
    
    
    
        /*--------------------以下为常用函数-------------
         ---------------------------------------------*/
        //处理文件大小表示方法
        function getSizeType(s)
        {
            var danwei = ["Byte","KB","MB","GB" ];
            var d = 0;
            while ( s >= 900 )
            {
                s = Math.round(s*100/1024)/100;
                d++;
            }
            return s+danwei[d];
        }
    
        function getFlashVars(parName){
            var parValue:String=stage.loaderInfo.parameters[parName];
            if(parValue==null)
                return "";
            else
                return parValue;
        }
    
    
    
        //转换乱码
        function EncodeUtf8(str : String):String {
            var oriByteArr : ByteArray = new ByteArray();
            oriByteArr.writeUTFBytes(str);
            var tempByteArr : ByteArray = new ByteArray();
            for (var i = 0; i<oriByteArr.length; i++) {
                if (oriByteArr[i] == 194) {
                    tempByteArr.writeByte(oriByteArr[i+1]);
                    i++;
                } else if (oriByteArr[i] == 195) {
                    tempByteArr.writeByte(oriByteArr[i+1] + 64);
                    i++;
                } else {
                    tempByteArr.writeByte(oriByteArr[i]);
                }
            }
            tempByteArr.position = 0;
            return tempByteArr.readMultiByte(tempByteArr.bytesAvailable,"chinese");
    //return tempByteArr.readMultiByte(tempByteArr.bytesAvailable,"chinese");
        }
    
    

    转载本站文章《flash上传文件的actionscript代码》,
    请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/AS/2015_0729_195.html