纯AS代码实现可预览本地图片的flash上传客户端

2012-01-17 10:23  评论 0 条

公司要求用flah制作一个上传头像的工具,不会怎么办呢?还是要做啊··所以在网上收集到的信息欢迎大家实验

需要Flash Player 10+版本的支持,原理就是主要利用fp10中的FileReference.load(),FileReference.data和Loader.loadBytes()三个方法通过图片加载到内存中,来实现预览本地图片,但这个方式不太适用大图片预览,图片越大内存消耗就越大。
[注意]
1.我这边图片上传路径是无效的,所以图片上传失败是正常的,你们可以改一下上传路径即可;
2.需要Flash Player 10的支持;
3.这次主要研究是预览本地图片功能。

 

  1. package project.test
  2. {
  3.     import flash.display.*;  
  4.     import flash.geom.Rectangle;  
  5.     import flash.net.*;  
  6.     import flash.text.*;  
  7.     import flash.filters.*;  
  8.     import flash.events.*;  
  9.     import flash.system.Security;  
  10.     import fl.controls.Button;  
  11.     import fl.controls.ProgressBar;  
  12.     import fl.controls.ProgressBarMode;  
  13.     /**
  14.      * @link kinglong@gmail.com
  15.      * @author Kinglong
  16.      * @playerversion fp10
  17.      */
  18.     [SWF(width="500", height="300", frameRate="24", backgroundColor="#FFFFFF")]
  19.     public class TestUpload extends Sprite {
  20.         private const DEFAULT_UPLOAD_PAGE:String = "http://test.klstudio.com/upload.asp";
  21.         private const BOX_WIDTH:uint = 500;
  22.         private const BOX_HEIGHT:uint = 300;
  23.         private const STATE_CACHE:String = "cache";
  24.         private const STATE_UPLOAD:String = "upload";
  25.         private var _filters:Array;
  26.         private var _file:FileReference;
  27.         private var _loader:Loader;
  28.         private var _progress:ProgressBar;
  29.         private var _state:String;
  30.         private var _buttons:Array;
  31.         private var _labels:Array;
  32.         private var _txts:Array;
  33.         private var _rect:Rectangle;
  34.         private var _state_txt:TextField;
  35.         public function TestUpload() {
  36.             Security.allowDomain("*");
  37.             _buttons = [];
  38.             _txts = [];
  39.             _labels = ["文件名称:","文件类型:","文件大小:","修改时间:"];
  40.             _rect = new Rectangle(20, 80, 180, 180);
  41.             _state = STATE_CACHE;
  42.             //背景;
  43.             this.graphics.beginFill(0x333333);
  44.             this.graphics.drawRoundRect(0, 0, BOX_WIDTH, BOX_HEIGHT, 10, 10);
  45.             this.graphics.endFill();
  46.             this.graphics.beginFill(0xEFEFEF);
  47.             this.graphics.drawRoundRect(1, 1, BOX_WIDTH - 2, BOX_HEIGHT - 2, 10, 10);
  48.             this.graphics.endFill();
  49.             this.graphics.beginFill(0x666666);
  50.             this.graphics.drawRoundRect(10, 30, BOX_WIDTH - 20, BOX_HEIGHT - 60, 20, 20);
  51.             this.graphics.endFill();
  52.             this.graphics.beginFill(0xFEFEFE);
  53.             this.graphics.drawRoundRect(11, 31, BOX_WIDTH - 22, BOX_HEIGHT - 62, 20, 20);
  54.             this.graphics.endFill();
  55.             this.graphics.beginFill(0xCCCCCC);
  56.             this.graphics.drawRect(11, 70, BOX_WIDTH - 22, 1);
  57.             this.graphics.endFill();
  58.             this.graphics.beginFill(0x000000);
  59.             this.graphics.drawRect(_rect.x-1, _rect.y-1, _rect.width+2, _rect.height+2);
  60.             this.graphics.endFill();
  61.             this.graphics.beginFill(0xEEEEEE);
  62.             this.graphics.drawRect(_rect.x, _rect.y, _rect.width, _rect.height);
  63.             this.graphics.endFill();
  64.             //标题;
  65.             var label:TextField;
  66.             label = getLabel("图片上传(预览图片版) by Kinglong", getTextFormat(0xFFFFFF, 14, true));
  67.             label.x = 10;
  68.             label.y = 5;
  69.             label.filters = [getLabelFilter(0x000000)];
  70.             this.addChild(label);
  71.             for (var i:uint = 0; i < _labels.length; i++ ) {
  72.                 label = getLabel(_labels[i], getTextFormat(0x333333, 12), false, false);
  73.                 label.x = _rect.right+5;
  74.                 label.y = _rect.y + 25 * i;
  75.                 label.width = 280;
  76.                 label.height = 20;
  77.                 _txts.push(label);
  78.                 this.addChild(label);
  79.             }
  80.             _state_txt = getLabel("状态:", getTextFormat(0x333333, 12));
  81.             _state_txt.x = 10;
  82.             _state_txt.y = BOX_HEIGHT - _state_txt.height - 5;
  83.             this.addChild(_state_txt);
  84.             //按钮;
  85.             var button:Button;
  86.             button = getButton("选择文件", 80);
  87.             button.move(20, 40);
  88.             button = getButton("上传文件", 80);
  89.             button.move(105, 40);
  90.             button.enabled = false;
  91.             //进度条;
  92.             _progress = new ProgressBar();
  93.             _progress.move(190, 40);
  94.             _progress.setSize(290,22);
  95.             _progress.mode = ProgressBarMode.MANUAL;
  96.             this.addChild(_progress);
  97.             //文件类型;
  98.             _filters = [];
  99.             var filter:FileFilter;
  100.             filter = new FileFilter("所有支持图片文件(*.jpg,*.jpeg,*.gif,*.png)", "*.jpg;*.jpeg;*.gif;*.png");
  101.             _filters[_filters.length] = filter;
  102.             filter = new FileFilter("JPEG files(*.jpg,*.jpeg)","*.jpg;*.jpeg");
  103.             _filters[_filters.length] = filter;
  104.             filter = new FileFilter("GIF files (*.gif)","*.gif");
  105.             _filters[_filters.length] = filter;
  106.             filter = new FileFilter("PNG files(*.png)","*.png");
  107.             _filters[_filters.length] = filter;
  108.             _file = new FileReference();
  109.             _file.addEventListener(Event.COMPLETE, fileHandler);
  110.             _file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fileHandler);  
  111.             _file.addEventListener(Event.SELECT, fileHandler);
  112.             _file.addEventListener(Event.OPEN, fileHandler);
  113.             _file.addEventListener(ProgressEvent.PROGRESS, fileHandler);
  114.             _file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileHandler);
  115.             _file.addEventListener(IOErrorEvent.IO_ERROR, fileHandler);
  116.             _file.addEventListener(HTTPStatusEvent.HTTP_STATUS, fileHandler);
  117.             _loader = new Loader();
  118.             _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
  119.             this.addChild(_loader);
  120.         }
  121.         public function get state():String {
  122.             return _state;
  123.         }
  124.         private function clickHandler(event:MouseEvent):void {
  125.             switch(event.target) {
  126.                 case _buttons[0]:
  127.                     _file.browse(_filters);
  128.                     break;
  129.                 case _buttons[1]:
  130.                     _file.upload(new URLRequest(DEFAULT_UPLOAD_PAGE));
  131.                     _state = STATE_UPLOAD;
  132.                     _buttons[0].enabled = false;
  133.                     _buttons[1].enabled = false;
  134.                     break;
  135.             }
  136.         }
  137.         private function loadHandler(event:Event):void {
  138.             _loader.scaleX = _loader.scaleY = 1;
  139.             var w:uint = _loader.width;
  140.             var h:uint = _loader.height;
  141.             if (w > _rect.width || h > _rect.height) {
  142.                 var ip:Number = w / h;
  143.                 var lp:Number = _rect.width / _rect.height;
  144.                 _loader.width = (ip > lp)?_rect.width:_rect.height*ip;
  145.                 _loader.height = (ip > lp)?_rect.width / ip:_rect.height;
  146.             }
  147.             _loader.x = _rect.x + (_rect.width - _loader.width) / 2;
  148.             _loader.y = _rect.y + (_rect.height - _loader.height) / 2;
  149.             _loader.visible = true;
  150.         }
  151.         private function fileHandler(event:Event):void {
  152.             switch(event.type) {
  153.                 case Event.COMPLETE:
  154.                     if(state == STATE_CACHE){
  155.                         _loader.loadBytes(_file.data);  
  156.                     }
  157.                     break;
  158.                 case DataEvent.UPLOAD_COMPLETE_DATA:  
  159.                     debug("图片上传完成!");
  160.                     _buttons[0].enabled = true;
  161.                     _buttons[1].enabled = false;
  162.                     _progress.setProgress(0, 1);
  163.                     break;
  164.                 case Event.SELECT:
  165.                     _txts[0].text = _labels[0] + _file.name;
  166.                     _txts[1].text = _labels[1] + _file.type;
  167.                     _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");
  168.                     _txts[3].text = _labels[3] + date2str(_file.modificationDate);
  169.                     _buttons[0].enabled = true;
  170.                     _buttons[1].enabled = true;
  171.                     _file.load();
  172.                     _state = STATE_CACHE;
  173.                     _loader.visible = false;
  174.                     debug("图片已经准备!");
  175.                     break;
  176.                 case Event.OPEN:
  177.                     if(state == STATE_UPLOAD){
  178.                         debug("正在上传图片...");
  179.                     }
  180.                     break;
  181.                 case ProgressEvent.PROGRESS:
  182.                     if (state == STATE_UPLOAD) {
  183.                         var pEvent:ProgressEvent = event as ProgressEvent;
  184.                         _progress.setProgress(pEvent.bytesLoaded, pEvent.bytesTotal);
  185.                     }
  186.                     break;
  187.                 case SecurityErrorEvent.SECURITY_ERROR:
  188.                 case IOErrorEvent.IO_ERROR:
  189.                 case HTTPStatusEvent.HTTP_STATUS:
  190.                     if (state == STATE_UPLOAD) {
  191.                         debug("图片上传失败!");
  192.                         _buttons[0].enabled = true;
  193.                         _buttons[1].enabled = true;
  194.                     }else {
  195.                         debug("图片缓冲失败!");
  196.                     }
  197.                     _progress.setProgress(0, 1);
  198.                     break;
  199.             }
  200.         }
  201.         private function getButton(lbl:String,width:uint=120):Button {
  202.             var button:Button = new Button();
  203.             button.label = lbl;
  204.             button.setSize(width, 22);
  205.             button.setStyle("textFormat", getTextFormat());
  206.             button.setStyle("disabledTextFormat", getTextFormat(0x999999));
  207.             button.setStyle("textPadding",4);
  208.             button.addEventListener(MouseEvent.CLICK, clickHandler);
  209.             this.addChild(button);
  210.             _buttons.push(button);
  211.             return button;
  212.         }
  213.         private function getLabel(label:String, format:TextFormat, selectable:Boolean = false, autoSize:Boolean = true):TextField {
  214.             var lbl:TextField = new TextField();
  215.             lbl.selectable = selectable;
  216.             lbl.defaultTextFormat = format;
  217.             if(autoSize){
  218.                 lbl.autoSize = TextFieldAutoSize.LEFT;
  219.             }
  220.             lbl.text = label;
  221.             return lbl;
  222.         }
  223.         private function getTextFormat(color:uint=0x000000,size:uint = 12,bold:Boolean=false):TextFormat {
  224.             var format:TextFormat = new TextFormat();
  225.             format.font = "宋体";
  226.             format.color = color;
  227.             format.size = size;
  228.             format.bold = bold;
  229.             return format;
  230.         }
  231.         private function getLabelFilter(color:uint=0xFFFFFF):BitmapFilter {
  232.             var alpha:Number = 0.8;
  233.             var blurX:Number = 2;
  234.             var blurY:Number = 2;
  235.             var strength:Number = 3;
  236.             var inner:Boolean = false;
  237.             var knockout:Boolean = false;
  238.             var quality:Number = BitmapFilterQuality.HIGH;
  239.             return new GlowFilter(color,
  240.                                   alpha,
  241.                                   blurX,
  242.                                   blurY,
  243.                                   strength,
  244.                                   quality,
  245.                                   inner,
  246.                                   knockout);
  247.         }
  248.         private function date2str(day:Date):String {
  249.             var str:String = day.getFullYear() + "-";
  250.             str += num2str(day.getMonth() + 1) + "-";
  251.             str += num2str(day.getDate()) + " ";
  252.             str += num2str(day.getHours()) + ":";
  253.             str += num2str(day.getMinutes()) + ":";
  254.             str += num2str(day.getSeconds());
  255.             return str;
  256.         }
  257.         private function num2str(val:Number):String {
  258.             var str:String = "00" + val;
  259.             return str.substr(str.length - 2, 2);
  260.         }
  261.         private function debug(message:String):void {
  262.             _state_txt.text = message;
  263.         }
  264.     }
  265. }

历史上的今天:

本文地址:http://blog.zd178.com/?p=451
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
新年快乐
新年快乐

发表评论


表情