greyimg

ImageUploader Class an Adaptor to FileReference with Image Width and Height Detection

Posted by admin in September 23rd, 2009
Published in Action Script 3.0, Tutorial

In many case I want flash Images uploader form detect not just filesize or filename, but also can detect Image width and Height. So I can alert user to upload a correct Image size before start uploading. I know we can crop or resize Image with server script processing, but in some reason keep user to upload a correct Image size also a good choice. The idea of this class is to load the files locally as ByteArray, then after converted to BitmapData, we can get Image width and Height value.

package com.janumedia.util 
{
	import com.janumedia.event.CustomEventDispatcher;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
 
	/**
	 * ...
	 * @author I Nengah Januartha
	 * @http://www.janumedia.com
	 */
 
	public class ImageUploader extends CustomEventDispatcher
	{
		public static const ERROR_FILENAME_CONTAIN_SPACE:String = "ErrorFileNameContainSpace";
 
		public var name:String;
		public var data:ByteArray;
		public var size:Number;
		public var width:Number;
		public var height:Number;
 
		private var _fileRef:FileReference;
		private var _loader:Loader;
		private var _fileTypes:Array;
 
		public function ImageUploader () 
		{
			super ();
 
			_fileRef = new FileReference ();
			_fileRef.addEventListener (Event.SELECT, handle_file_select, false, 0, true);
			_fileRef.addEventListener (Event.COMPLETE, handle_file_load, false, 0, true);
			_fileRef.addEventListener (IOErrorEvent.IO_ERROR, handle_file_upload_io_error, false, 0, true);
			_fileRef.addEventListener (SecurityErrorEvent.SECURITY_ERROR, handle_file_upload_security_error, false, 0, true);
			_fileRef.addEventListener (ProgressEvent.PROGRESS, handle_file_upload_progress, false, 0, true);
			_fileRef.addEventListener (DataEvent.UPLOAD_COMPLETE_DATA, handle_file_upload_complete, false, 0, true);
 
			_loader = new Loader ();
			_loader.contentLoaderInfo.addEventListener (Event.COMPLETE, handle_file_load_locally, false, 0, true);
		}
 
		public function browse (typeFilter:Array) : void
		{
			_fileRef.browse (typeFilter);
		}
 
		public function upload (url:URLRequest) : void
		{
			_fileRef.upload (url);
		}
 
		private function handle_file_select (e:Event) : void
		{
			// verify filename, filename with contain space will NOT allowed
			var regExp:RegExp = new RegExp (" ", "g");
			if (regExp.exec (_fileRef.name))
			{
				dispatchEvent (new Event (ERROR_FILENAME_CONTAIN_SPACE));
 
				return;
			}
 
			name = _fileRef.name;
			size = _fileRef.size;
 
			// load locally and get file info
			_fileRef.load ();
		}
 
		private function handle_file_load (e:Event) : void
		{
			data = _fileRef.data;
 
			_loader.loadBytes (_fileRef.data);
		}
 
		private function handle_file_load_locally (e:Event) : void
		{
			width = Bitmap (e.target.content).bitmapData.width;
			height = Bitmap (e.target.content).bitmapData.height;
 
			dispatchEvent (new Event (Event.SELECT));
		}
 
		private function handle_file_upload_io_error (e:IOErrorEvent) : void
		{
			dispatchEvent (new IOErrorEvent (IOErrorEvent.IO_ERROR));
		}
 
		private function handle_file_upload_security_error (e:SecurityErrorEvent) : void
		{
			dispatchEvent (new SecurityErrorEvent (SecurityErrorEvent.SECURITY_ERROR));
		}
 
		private function handle_file_upload_progress (e:ProgressEvent) : void
		{
			if (data) dispatchEvent (new ProgressEvent (ProgressEvent.PROGRESS));
		}
 
		private function handle_file_upload_complete (e:DataEvent) : void
		{
			dispatchEvent (new DataEvent (DataEvent.UPLOAD_COMPLETE_DATA));
		}
 
	}
 
}

No user Responded In This Post

Follow-up this post comment rss or leave a trackback

Leave A Reply Below

Currently browsing ImageUploader Class an Adaptor to FileReference with Image Width and Height Detection

 Username (*required)

 Email Address (*private)

 Website (*optional)


Add to Technorati Favorites

What I am doing

UserOnline

 

September 2009
M T W T F S S
« Nov   Dec »
 123456
78910111213
14151617181920
21222324252627
282930  

Archives

Tags

Meta

Co-worker

Community

Personal

Profesional

Resources

Wordpress