FLASH UPLOADERのコード

今回追加したのは画像のアップロード前のPreview。
FileReference.browseでファイル選択後、FileReference.loadでロードするとFileReference.dataにBytesArrayで格納されるので、それをLoader.loadBytesでロードする。あとはLoaderを通常の外部ファイルの時と同様にaddChildする。

以下コードです。

ライブラリアイテムってのはflaにあるボタンとかのシンボルです。
あと、Tweensyちょっとだけ使ってます。
Tweensyはもっとちゃんと使い込んでみたい。

//AS3///////////////////////////////////////////////////////////////////////////
// 
// Flash Uploader Version 2.00
// 
////////////////////////////////////////////////////////////////////////////////
 
package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.Loader;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.events.*;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
 
	import com.flashdynamix.motion.Tweensy;
	import com.flashdynamix.motion.TweensyGroup;
//	import com.flashdynamix.utils.SWFProfiler;
 
 
	public class Main extends MovieClip {
		private const UPLOAD_URL:String = "fup.php";
		private const PREVIEW_WIDTH:Number = 200;
		private const PREVIEW_HEIGHT:Number = 200;
 
		private var fr:FileReference;
		private var loader:Loader;
		private var previewContainer:Sprite;
		private var fileName:TextField;
		private var tg:TweensyGroup;
 
		// ライブラリアイテム
		private var browseBtn:BrowseBtn;
		private var uploadBtnOn:UploadBtnOn;
		private var uploadBtnOff:UploadBtnOff;
		private var logo:Logo;
		private var fileNameBG:FileName;
		private var overlay:Overlay;
		private var bar:ProcessingBar;
		private var up:UploadProcessing;
		private var ue:UploadError;
		private var uc:UploadCompleted;
 
		public function Main() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
 
			//SWFProfiler.init(this);
 
			fr = new FileReference();
			fr.addEventListener(Event.SELECT, selectFile);
 
			logo = new Logo();
			logo.x = 34;
			logo.y = 197;
			logo.addEventListener(MouseEvent.CLICK, clickLogo);
			addChild(logo);
 
			previewContainer = new Sprite();
			previewContainer.x = 159;
			previewContainer.y = 10;
			addChild(previewContainer);
 
			browseBtn = new BrowseBtn();
			browseBtn.x = 8;
			browseBtn.y = 8;
			browseBtn.addEventListener(MouseEvent.CLICK, browseFile);
			browseBtn.buttonMode = true;
			addChild(browseBtn);
 
			fileNameBG = new FileName();
			fileNameBG.x = 10;
			fileNameBG.y = 63;
 
			fileName = new TextField();
			fileName.width = 130;
			fileName.x = 3;
			fileName.y = 3;
			var format:TextFormat = new TextFormat();
			format.color = 0xdbdbdb;
			format.size = 18;
			format.font = "_ゴシック";
			fileName.defaultTextFormat = format;
			fileName.text = "FileName";
 
			fileNameBG.addChild(fileName);
			addChild(fileNameBG);
 
			uploadBtnOff = new UploadBtnOff();
			uploadBtnOff.x = 8;
			uploadBtnOff.y = 102;
			uploadBtnOff.name = "uploadBtnOff";
			addChild(uploadBtnOff);
 
			overlay = new Overlay();
			overlay.x = 1;
			overlay.y = 1;
			overlay.alpha = 0;
 
			bar = new ProcessingBar();
			bar.x = 85;
			bar.y = 102;
			bar.width = 0;
 
			up = new UploadProcessing();
			up.x = 91;
			up.y = 81;
			up.alpha = 0;
 
			ue = new UploadError();
			ue.x = 117;
			ue.y = 81;
			ue.alpha = 0;
 
			uc = new UploadCompleted();
			uc.x = 93;
			uc.y = 81;
			uc.alpha = 0;
		}
 
		//---------------------------------------
		// ファイル選択
		//---------------------------------------
		private function browseFile(e:MouseEvent) {
			fr.browse();
		}
 
		//---------------------------------------
		// ファイル選択後処理
		//---------------------------------------
		private function selectFile(e:Event = null) {
			trace("SELECTED FILE!!");
			fr.addEventListener(Event.COMPLETE, loadComplete);
			fr.load();
		}
 
		//---------------------------------------
		// 選択ファイルのPreview
		//---------------------------------------
		private function loadComplete(e:Event) {
			trace("LOAD COMPLETE!!");
			trace(fr.name);
			fr.removeEventListener(Event.COMPLETE, loadComplete);
 
			var regExp:RegExp = new RegExp("gif|jpg|jpeg|png|swf$", "i");
			if(regExp.test(fr.name)) {
				if(previewContainer.getChildByName("previewLoader") == null) {
					loader = new Loader();
					loader.name = "previewLoader";
					trace("PREVIEW LOADER")
				}
 
				var appDomain = new ApplicationDomain();
				var context = new LoaderContext();
				context.applicationDomain = appDomain;
				loader.loadBytes(fr.data, context);
				loader.contentLoaderInfo.addEventListener(Event.INIT, initLoadFile);
			}
			else {
				fileName.text = fr.name;
				setUploadBtn();
			}
		}
 
		//---------------------------------------
		// FileReference.load時のinitイベント
		//---------------------------------------
		private function initLoadFile(e:Event) {
			var ratio:Number = 0;
			trace("loader.content.width : " + loader.content.width);
			if(loader.content.width > loader.content.height) {
				ratio = PREVIEW_WIDTH / loader.content.width;
				loader.content.width = PREVIEW_WIDTH;
				loader.content.height = loader.content.height * ratio
			}
			else {
				ratio = PREVIEW_HEIGHT / loader.content.height;
				loader.content.height = PREVIEW_HEIGHT;
				loader.content.width = loader.content.width * ratio;
			}
			loader.x = (PREVIEW_WIDTH - loader.content.width) * 0.5;
			loader.y = (PREVIEW_HEIGHT - loader.content.height) * 0.5;
			previewContainer.addChild(loader);
 
			fileName.text = fr.name;
 
			setUploadBtn();
 
			if(getChildByName("uploadBtnOff") != null) {
				removeChild(uploadBtnOff);
			}
		}
 
		//---------------------------------------
		// UPLOADボタンのアクティブ化
		//---------------------------------------
		private function setUploadBtn() {
			if(getChildByName("uploadBtnOn") == null) {
				uploadBtnOn = new UploadBtnOn();
				uploadBtnOn.x = 8;
				uploadBtnOn.y = 102;
				uploadBtnOn.name = "uploadBtnOn";
				uploadBtnOn.buttonMode = true;
				uploadBtnOn.addEventListener(MouseEvent.CLICK, uploadBtnEvent);
				addChild(uploadBtnOn);
			}
		}
 
		//---------------------------------------
		// アップロードボタンイベント
		//---------------------------------------
		private function uploadBtnEvent(e:MouseEvent) {
			fr.addEventListener(Event.COMPLETE, uploadComplete);
			fr.addEventListener(Event.OPEN, uploadStartEvent);
			fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataEvent);
			fr.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusEvent);
			fr.addEventListener(IOErrorEvent.IO_ERROR, uploadIoErrorEvent);
			fr.addEventListener(ProgressEvent.PROGRESS, progressEvent);
			fr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorEvent);
			fr.upload(new URLRequest(UPLOAD_URL));
 
			overlay.addChild(up);
			up.alpha = 1;
 
			addChild(overlay);
			Tweensy.to(overlay, {alpha: 1}, 0.5);
			addChild(bar);
		}
 
		//---------------------------------------
		// OPENイベント
		//---------------------------------------
		private function uploadStartEvent(e:Event) {
			trace("OPEN!!");
		}
 
		//---------------------------------------
		// HTTPStatusEvent
		//---------------------------------------
		private function httpStatusEvent(e:HTTPStatusEvent) {
			trace("HTTP STATUS");
		}
 
		//---------------------------------------
		// IOErrorEvent
		//---------------------------------------
		private function uploadIoErrorEvent(e:IOErrorEvent) {
			trace("IO ERROR : " + e.text);
			viewError();
		}
 
		//---------------------------------------
		// ProgressEvent
		//---------------------------------------
		private function progressEvent(e:ProgressEvent) {
			bar.width = 200 * (e.bytesLoaded / e.bytesTotal);
		}
 
		//---------------------------------------
		// SecurityErrorEvent
		//---------------------------------------
		private function securityErrorEvent(e:SecurityErrorEvent) {
			trace("SECURITY ERROR : " + e.text);
			viewError();
		}
 
		//---------------------------------------
		// UploadCompleteData Event
		//---------------------------------------
		private function uploadCompleteDataEvent(e:DataEvent) {
			trace("UPLOAD COMPLETE DATA");
		}
 
		//---------------------------------------
		// アップロード完了イベント
		//---------------------------------------
		private function uploadComplete(e:Event) {
			fr.removeEventListener(Event.COMPLETE, uploadComplete);
 
			bar.width = 200;
			overlay.addChild(uc);
 
			tg = new TweensyGroup();
			tg.alphaTo(up, 0, 0.5);
			tg.alphaTo(uc, 1, 0.5);
			tg.onComplete = function() {
				overlay.removeChild(up);
			}
 
			//Tweensy.to(overlay, {alpha: 0}, 0.5, null, 1.5, null, removeOverlay);
			tg = new TweensyGroup();
			tg.alphaTo(overlay, 0, 0.5, null, 1.5);
			tg.alphaTo(bar, 0, 0.5, null, 1.5);
			tg.onComplete = removeOverlay;
			trace("COMPLETE UPLOAD!!");
		}
 
		private function removeOverlay() {
			overlay.removeChild(uc);
			removeChild(bar);
			removeChild(overlay);
		}
 
		//---------------------------------------
		// エラー表示
		//---------------------------------------
		private function viewError() {
			overlay.addChild(ue);
 
			tg = new TweensyGroup();
			tg.alphaTo(up, 0, 0.5);
			tg.alphaTo(ue, 1, 0.5);
			tg.onComplete = function() {
				overlay.removeChild(up);
			}
		}
 
		private function clickLogo(e:MouseEvent) {
			navigateToURL(new URLRequest("http://eternitydesign.net/"), "_blank");
		}
	}
}

トラックバック

http://blog.eternitydesign.net/archives/2009/04/flash-uploader.html/trackback