How to Load File by browse in Falsh As 3.0 (File Refference In As 3.0)

Hi,

Here I am writing about "File Refference" Concept in Flash Action Script 3.0.

We can use "File Refference class" to "Save or create", "upload" and "browse and load" file "from or to" flash.

This example for "browse and load" using As 3.0.

Note: It will work on "Flash Player 10 and later versions" only and you are "not able get path" of file because Adobe remove this feature for security

purpose. (Flash player 10 avail in Flash Cs4 and later versions).



To follow this example first create a button with name "imgLoad" and Copy below code:


var fileRef:FileReference= new FileReference();
imgLoad.addEventListener(MouseEvent.CLICK, onButtonClick);

function onButtonClick(e:MouseEvent):void {
    fileRef.browse([new FileFilter("All Files (*.swf,*.jpg,*.jpeg,*.gif,*.png,*.tif)","*.swf;*.jpg;*.jpeg;*.gif;*.png;*.tif")]);
    fileRef.addEventListener(Event.SELECT, onFileSelected);
}

function onFileSelected(e:Event):void {
    fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
    fileRef.load();
}




function onFileLoaded(event:Event)
{
    //loader object containing image data
    var loader:Loader = new Loader();
    loader.loadBytes(event.target.data);
    loader.x=0;
    loader.y=0;
    stage.addChild(loader);
}





Explination:

In above code first line "var fileRef:FileReference= new FileReference();" creating File refference object. Second line We adding listener to button

"imgLoad" to browse file.


In "onButtonClick" function we are written "fileRef.browse" to browse file. We have mentioned here load "swf,jpg,jpeg,gif,png,tif" files only we can load by

adding fileter in browse. We have freedom to write extensions :).


"fileRef.addEventListener(Event.SELECT, onFileSelected)" Line will excecute slect a file and click on open. And it will call funciton "onFileSelected". In

this function we will load file into flash and after loading it will call to function "onFileLoaded".


In "onFileLoaded" function we are adding image into stage from Loader data.


For More Information on File Refference you can go through link:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#save%28%29



Thanks,
K Swamy Vishnubhatla.

No comments:

Post a Comment

Thanks for comment, We are professionals in Web Designing, Flash, E-learning, Logos Designing, Templates Making, Business Cards, Flash Banners, Flash Components

If you need any of assistance, please contact to swamy.webdesigner@gmail.com

Note: Only a member of this blog may post a comment.