因公司项目需求,在客户端需要用户能够上传图片,为此我又写了一遍这个flash上传的小玩意,其实这个东东我已经写了好几个,但每次写时隔许久,每次写有要回忆(我的记性不好,所以总是记不住细节,好在原理还是忘不了,写写改改就出来了),现在发一遍,大家能用上的就拿去吧。
package {
import com.adobe.serialization.json.JSON;
import com.flash.external.Delegate;
import com.flash.uploader.utils.Base64;
import com.flash.uploader.utils.JPEGEncoder;
import flash.display.MovieClip;
import flash.net.FileReference;
import flash.display.Loader;
import fl.controls.Button;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.display.DisplayObject;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.external.ExternalInterface;
import flash.system.Security;
[SWF(width="216", height="230", frameRate="24", backgroundColor="#FFFFFF")]
public class Main extends MovieClip {
private const UPLOAD_IMAGE_URL:String = "后台接口";
private const MAX_FILE_SIZE_IN_KB:int = 2048;
private const BOX_WIDTH:uint = 216;
private const BOX_HEIGHT:uint = 205;
private var invisible:Sprite;
private var _label:TextField;
private var _format:TextFormat;
private var onLoadBtn:Button;
private var _file:FileReference;
private var _filter:FileFilter;
private var _loader:Loader;
private var _urlRequest:URLRequest;
private var _urlData:URLVariables;
private var _urlLoader:URLLoader;
private var _result:Object;
private var fileSize:Number;
public function Main() {
super();
Security.allowDomain("xxx.com");
//label;
_label = createLabel('Please load your image, press here',25,50,160,200)
this.addChild(_label);
//invisible btn
invisible = new Sprite();
invisible.graphics.beginFill(0x000000,.3);
invisible.graphics.drawRect(1,1,BOX_WIDTH,BOX_HEIGHT);
invisible.graphics.endFill();
this.addChild(invisible);
invisible.addEventListener(MouseEvent.CLICK, clickSelect);
//onLoadBtn
onLoadBtn = createBtn('start uploding',2,207,100,20);
this.addChild(onLoadBtn);
onLoadBtn.enabled = false;
//_filter;
_filter = new FileFilter("Support All Images:(*.jpeg,*.jpg,*.gif,*.png)","*.jpeg;*.jpg;*.gif;*.png");
}
private function clickSelect(event:MouseEvent):void{
_file = new FileReference();
_file.addEventListener(Event.SELECT, onSelect);
_file.browse([_filter]);
}
private function onSelect(event:Event):void{
trace('select');
_file.removeEventListener(Event.SELECT, onSelect);
_file.load();
_file.addEventListener(ProgressEvent.PROGRESS, onProgress);
_file.addEventListener(Event.COMPLETE, onLoadFinish);
}
private function onLoadFinish(event:Event):void{
_file.removeEventListener(Event.COMPLETE, onLoadFinish);
_loader = new Loader();
_loader.loadBytes(_file.data);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
}
private function loadComplete(event:Event):void{
_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
/* check file size */
fileSize = Math.round(_file.size / 1024);
if(fileSize > MAX_FILE_SIZE_IN_KB){
alertHandler("Your file must be under " +
MAX_FILE_SIZE_IN_KB / 1024 +
"MB and in one of the following formats: *.JPEG,*.JPG,*.GIF,*.PNG");
}
else{
//for better display img in a proper size;
trace(_loader.content.width+'||'+_loader.content.height);
if(_loader.content.width>BOX_WIDTH||_loader.content.height>BOX_HEIGHT){
if(_loader.content.width<_loader.content.height){
_loader.content.width = _loader.content.width *(BOX_HEIGHT/_loader.content.height);
_loader.content.height = BOX_HEIGHT;
}
else if(_loader.content.width>_loader.content.height){
trace("width xiaoyu height")
_loader.content.height = _loader.content.height *(BOX_WIDTH/_loader.content.width);
_loader.content.width = BOX_WIDTH;
}
}
//center position image;
var varX:int = (BOX_WIDTH - _loader.content.width)/2;
var varY:int = (BOX_HEIGHT - _loader.content.height)/2;
_loader.x = varX;
_loader.y = varY;
//empty files
makeHoldEmpty(invisible);
invisible.addChild(_loader);
_label.visible = false;
onLoadBtn.enabled = true;
onLoadBtn.addEventListener(MouseEvent.CLICK, startUpload);
}
trace('name:'+_file.name+'\rtyep:'+_file.type+'\rcreator:'+_file.creator+'\rsize:'+_file.size);
}
private function startUpload(event:MouseEvent):void{
onLoadBtn.removeEventListener(MouseEvent.CLICK, startUpload);
_urlRequest = new URLRequest(UPLOAD_IMAGE_URL);
_urlRequest.method = URLRequestMethod.POST;
_urlData = new URLVariables;
_urlData.base64image = Base64.encode(_file.data);
_urlRequest.data = _urlData;
_urlLoader = new URLLoader()
_urlLoader.addEventListener(Event.COMPLETE, uploadComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
try{
_urlLoader.load(_urlRequest)
}
catch(event){
errorHandler(null);
}
trace('startUpload');
}
private function uploadComplete(event:Event):void{
_urlLoader.removeEventListener(Event.COMPLETE, uploadComplete);
_result = JSON.decode(event.currentTarget.data);
if (ExternalInterface.available)
{
ExternalInterface.call("uploadFileCallback", _result);//here you can pass some parameters for debuging in browser.
}
}
//error Handler;
private function errorHandler(event:Event){
if (event.currentTarget.hasEventListener(IOErrorEvent.IO_ERROR))
event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
if (event.currentTarget.hasEventListener(SecurityErrorEvent.SECURITY_ERROR))
event.currentTarget.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
if (ExternalInterface.available)
{
//ExternalInterface.call("uploadFileCallback",{success:false,message:null});
}
}
private function onProgress(event:ProgressEvent){
//具体你想加progress你就自己加吧;
}
private function alertHandler(value):void
{
Delegate.instance().jsAlert(value);
}
//create label
private function createLabel(context:String,posx:int,posy:int,width:int=200,height:int=200):TextField{
var _label:TextField = new TextField();
_label.text = context;
_format = new TextFormat('Arial',14,0x999999);
_label.wordWrap = true;
_label.setTextFormat(_format);
_label.x = posx;
_label.y = posy;
_label.width = width;
_label.height = height;
return _label;
}
//create label
private function createBtn(label:String,posx:int,posy:int,w:int = 50, h:int = 10):Button{
var btn:Button = new Button();
btn.label = label;
btn.width = w;
btn.height = h;
btn.x = posx;
btn.y = posy;
return btn;
}
//check number for uploaded img;
private function makeHoldEmpty(target:Sprite):void{
//remove all history children;
var numChild:int = target.numChildren;
for(var i:int = 0; i<numChild; i++){
target.removeChildAt(i);
trace(i);
}
}
}
}