flashas3.0:纯AS代码实现可预览本地图片的flash上传客户端(as3.0)

  需要Flash Player 10+版本支持原理就是主要利用fp10中FileReference.loadFileReference.data和 Loader.loadBytes 3个思路方法通过图片加载到内存中来实现预览本地图片但这个方式不太适用大图片预览图片越大内存消耗就越大

  [注意]:

  1.我这边图片上传路径是无效所以图片上传失败是正常你们可以改下上传路径即可;

  2.需要Flash Player 10支持;

  3.这次主要研究是预览本地图片功能

  实现代码:

viewplaincopytoclipboardpr?
packageproject.test  
{  
  importflash.display.*;  
  importflash.geom.Rectangle;  
  importflash.net.*;  
  importflash.text.*;  
  importflash.filters.*;  
  importflash.events.*;  
  importflash.system.Security;  
    
  importfl.controls.Button;  
  importfl.controls.ProgressBar;  
  importfl.controls.ProgressBarMode;  
    
  /** 
  *@[email protected] 
  *@authorKinglong 
  *@playerversionfp10   
  */  
  [SWF(width="500",height="300",frameRate="24",backgroundColor="#FFFFFF")]  
  publicTestUploadextendsSprite{  
      
    privateconstDEFAULT_UPLOAD_PAGE:String="http://test.klstudio.com/upload.asp";     
    privateconstBOX_WIDTH:u=500;  
    privateconstBOX_HEIGHT:u=300;  
      
    privateconstSTATE_CACHE:String="cache";  
    privateconstSTATE_UPLOAD:String="upload";  
      
    privatevar_filters:Array;  
    privatevar_file:FileReference;  
    privatevar_loader:Loader;  
    privatevar_progress:ProgressBar;  
    privatevar_state:String;  
    privatevar_buttons:Array;  
    privatevar_labels:Array;  
    privatevar_txts:Array;  
    privatevar_rect:Rectangle;  
    privatevar_state_txt:TextField;  
      
    publicfunctionTestUpload{  
      Security.allowDo("*");  
        
      _buttons=;  
      _txts=;  
      _labels=["文件名称:","文件类型:","文件大小:","修改时间:"];  
        
      _rect=Rectangle(20,80,180,180);  
      _state=STATE_CACHE;  
        
      //背景;  
      this.graphics.beginFill(0x333333);  
      this.graphics.drawRoundRect(0,0,BOX_WIDTH,BOX_HEIGHT,10,10);  
      this.graphics.endFill;  
      this.graphics.beginFill(0xEFEFEF);  
      this.graphics.drawRoundRect(1,1,BOX_WIDTH-2,BOX_HEIGHT-2,10,10);  
      this.graphics.endFill;  
      this.graphics.beginFill(0x666666);  
      this.graphics.drawRoundRect(10,30,BOX_WIDTH-20,BOX_HEIGHT-60,20,20);       
      this.graphics.endFill;  
      this.graphics.beginFill(0xFEFEFE);  
      this.graphics.drawRoundRect(11,31,BOX_WIDTH-22,BOX_HEIGHT-62,20,20);  
      this.graphics.endFill;  
        
      this.graphics.beginFill(0xCCCCCC);  
      this.graphics.drawRect(11,70,BOX_WIDTH-22,1);  
      this.graphics.endFill;  
        
      this.graphics.beginFill(0x000000);  
      this.graphics.drawRect(_rect.x-1,_rect.y-1,_rect.width+2,_rect.height+2);  
      this.graphics.endFill;        
      this.graphics.beginFill(0xEEEEEE);  
      this.graphics.drawRect(_rect.x,_rect.y,_rect.width,_rect.height);  
      this.graphics.endFill;  
        
        
      //标题;  
      varlabel:TextField;        
      label=getLabel("图片上传(预览图片版)byKinglong",getTextFormat(0xFFFFFF,14,true));  
      label.x=10;  
      label.y=5;  
      label.filters=[getLabelFilter(0x000000)];  
      this.addChild(label);  
        
      for(vari:u=0;i<_labels.length;i){      
        label=getLabel(_labels[i],getTextFormat(0x333333,12),false,false);          
        label.x=_rect.right+5;  
        label.y=_rect.y+25*i;  
        label.width=280;  
        label.height=20;  
        _txts.push(label);  
        this.addChild(label);  
      }       
        
      _state_txt=getLabel("状态:",getTextFormat(0x333333,12));  
      _state_txt.x=10;  
      _state_txt.y=BOX_HEIGHT-_state_txt.height-5;  
      this.addChild(_state_txt);  
        
      //按钮;  
      varbutton:Button;  
      button=getButton("选择文件",80);      
      button.move(20,40);  
        
      button=getButton("上传文件",80);      
      button.move(105,40);  
      button.enabled=false;  
        
      //进度条;  
      _progress=ProgressBar;  
      _progress.move(190,40);  
      _progress.Size(290,22);  
      _progress.mode=ProgressBarMode.MANUAL;              
      this.addChild(_progress);  
        
      //文件类型;  
      _filters=;  
      varfilter:FileFilter;       
      filter=FileFilter("所有支持图片文件(*.jpg,*.jpeg,*.g,*.png)","*.jpg;*.jpeg;*.g;*.png");  
      _filters[_filters.length]=filter;  
      filter=FileFilter("JPEGfiles(*.jpg,*.jpeg)","*.jpg;*.jpeg");  
      _filters[_filters.length]=filter;  
      filter=FileFilter("GIFfiles(*.g)","*.g");  
      _filters[_filters.length]=filter;  
      filter=FileFilter("PNGfiles(*.png)","*.png");  
      _filters[_filters.length]=filter;  
        
      _file=FileReference;  
      _file.addEventListener(Event.COMPLETE,fileHandler);  
      _file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fileHandler);  
      _file.addEventListener(Event.SELECT,fileHandler);  
      _file.addEventListener(Event.OPEN,fileHandler);        
      _file.addEventListener(ProgressEvent.PROGRESS,fileHandler);  
      _file.addEventListener(SecurityErrorEvent.SECURITY_ERROR,fileHandler);  
      _file.addEventListener(IOErrorEvent.IO_ERROR,fileHandler);  
      _file.addEventListener(HTTPStatusEvent.HTTP_STATUS,fileHandler);  
        
      _loader=Loader;  
      _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadHandler);  
      this.addChild(_loader);  
    }  
      
    publicfunctiongetstate:String{  
      _state;  
    }  
      
    privatefunctionclickHandler(event:MouseEvent):void{  
      switch(event.target){  
        _buttons[0]:  
          _file.browse(_filters);  
          ;  
        _buttons[1]:  
          _file.upload(URLRequest(DEFAULT_UPLOAD_PAGE));  
          _state=STATE_UPLOAD;           
          _buttons[0].enabled=false;  
          _buttons[1].enabled=false;  
          ;  
      }  
    }  
      
    privatefunctionloadHandler(event:Event):void{  
      _loader.scaleX=_loader.scaleY=1;  
      varw:u=_loader.width;  
      varh:u=_loader.height;  
      (w>_rect.width||h>_rect.height){          
        varip:Number=w/h;  
        varlp:Number=_rect.width/_rect.height;      
        _loader.width=(ip>lp)?_rect.width:_rect.height*ip;  
        _loader.height=(ip>lp)?_rect.width/ip:_rect.height;  
      }  
      _loader.x=_rect.x+(_rect.width-_loader.width)/2;  
      _loader.y=_rect.y+(_rect.height-_loader.height)/2;       
      _loader.visible=true;  
    }  
      
    privatefunctionfileHandler(event:Event):void{  
      switch(event.type){  
        Event.COMPLETE:  
          (stateSTATE_CACHE){  
            _loader.loadBytes(_file.data);  
          }  
          ;  
        DataEvent.UPLOAD_COMPLETE_DATA:  
          debug("图片上传完成!");  
          _buttons[0].enabled=true;  
          _buttons[1].enabled=false;  
          _progress.Progress(0,1);  
          ;  
        Event.SELECT:  
          _txts[0].text=_labels[0]+_file.name;  
          _txts[1].text=_labels[1]+_file.type;  
          _txts[2].text=_labels[2]+((_file.size>1024*1024)?Math.round(_file.size*10/(1024*1024))/10 
+"MB":Math.round(_file.size*10/1024)/10+"KB");           
          _txts[3].text=_labels[3]+date2str(_file.modicationDate);  
          _buttons[0].enabled=true;  
          _buttons[1].enabled=true;  
          _file.load;  
          _state=STATE_CACHE;  
          _loader.visible=false;  
          debug("图片已经准备!");  
          ;  
        Event.OPEN:  
          (stateSTATE_UPLOAD){  
            debug("正在上传图片...");  
          }  
          ;  
        ProgressEvent.PROGRESS:  
          (stateSTATE_UPLOAD){  
            varpEvent:ProgressEvent=eventasProgressEvent;  
            _progress.Progress(pEvent.sLoaded,pEvent.sTotal);  
          }  
          ;  
        SecurityErrorEvent.SECURITY_ERROR:  
        IOErrorEvent.IO_ERROR:  
        HTTPStatusEvent.HTTP_STATUS:           
          (stateSTATE_UPLOAD){  
            debug("图片上传失败!");  
            _buttons[0].enabled=true;  
            _buttons[1].enabled=true;  
          }{  
            debug("图片缓冲失败!");  
          }  
          _progress.Progress(0,1);  
          ;  
          
      }  
    }  
      
    privatefunctiongetButton(lbl:String,width:u=120):Button{  
      varbutton:Button=Button;  
      button.label=lbl;  
      button.Size(width,22);   
      button.Style("textFormat",getTextFormat);  
      button.Style("disabledTextFormat",getTextFormat(0x999999));  
      button.Style("textPadding",4);  
      button.addEventListener(MouseEvent.CLICK,clickHandler);        
      this.addChild(button);  
      _buttons.push(button);  
      button;  
    }  
      
    privatefunctiongetLabel(label:String,format:TextFormat,selectable: 
Boolean=false,autoSize:Boolean=true):TextField{      
      varlbl:TextField=TextField;  
      lbl.selectable=selectable;  
      lbl.defaultTextFormat=format;  
      (autoSize){  
        lbl.autoSize=TextFieldAutoSize.LEFT;  
      }  
      lbl.text=label;  
      lbl;  
    }  
      
    privatefunctiongetTextFormat(color:u=0x000000,size:u=12,bold:Boolean=false): 
TextFormat{  
      varformat:TextFormat=TextFormat;  
      format.font="宋体";  
      format.color=color;  
      format.size=size;  
      format.bold=bold;  
      format;  
    }  
      
    privatefunctiongetLabelFilter(color:u=0xFFFFFF):BitmapFilter{  
      varalpha:Number=0.8;  
      varblurX:Number=2;  
      varblurY:Number=2;  
      varstrength:Number=3;  
      varinner:Boolean=false;  
      varknockout:Boolean=false;  
      varquality:Number=BitmapFilterQuality.HIGH;  
  
      GlowFilter(color,  
                 alpha,  
                 blurX,  
                 blurY,  
                 strength,  
                 quality,  
                 inner,  
                 knockout);  
    }  
      
    privatefunctiondate2str(day:Date):String{  
      varstr:String=day.getFullYear+"-";  
      strnum2str(day.getMonth+1)+"-";  
      strnum2str(day.getDate)+"";  
      strnum2str(day.getHours)+":";  
      strnum2str(day.getMinutes)+":";  
      strnum2str(day.getSeconds);  
      str;  
    }  
      
    privatefunctionnum2str(val:Number):String{  
      varstr:String="00"+val;  
      str.substr(str.length-2,2);       
    }  
      
    privatefunctiondebug(message:String):void{  
      _state_txt.text=message;  
    }  
      
  }  
    
}


Tags:  as3.0教程 as3.0 flashas3.0教程 flashas3.0

延伸阅读

最新评论

发表评论