Yes it is possible to upload Image in Flash Player 10 without use of server side scripting language


Today,I was just going through image upload process and I was amazed that we can upload the image in FP10 without use of any server side scripting language like PHP or ASP.The following code will give some idea on how this is possible

Consider you have a button named loadBtn on your stage

//this FileReference class has added the power to load and save images at runtime without any server side scripting language
var fileRef:FileReference;
var loader:Loader;
loadBtn.addEventListener(MouseEvent.CLICK,goLoadImage,false,0,true);
function goLoadImage(e:MouseEvent):void
{
fileRef = new FileReference();
//we need FileFilter so that while choosing image we can list out our needed file like jpg or png or gif or all
var fileFilter:FileFilter = new FileFilter("images","*.jpg;*.png.*.gif");
//now we use FileReference with FileFilter to browse the required images
fileRef.browse([fileFilter]);
//now we have to write an event handler function when user selects any image
fileRef.addEventListener(Event.SELECT,imageSelected);
}
function imageSelected(e:Event):void
{
//now make the FileReference to load the selected file
fileRef.addEventListener(Event.COMPLETE,loadImage);
fileRef.load();
}
function loadImage(e:Event):void
{
//the data thus loaded is in ByteArray so we have to use it like this
var ba:ByteArray = e.target.data;
//and we use loader to load that byte Array
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLTE,imageLoaded);
loader.loadBytes(ba);
}
function imageLoaded(e:Event):void
{
//the content of Loader is now takes as Bitmap and added to stage
var btmp:Bitmap = e.target.content as Bitmap;
this.addChild(btmp);
}

Isn’t this so simple and thanks to FP10 new feature.

If you want more advanced way to deal with image upload,look at this post
http://www.adylevy.com/index.php/2009/11/13/new-multiple-file-uploader-with-preview/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s