Here we will see how to use local files data in Flash. You can use any type of file but remember to convert it into proper data type. We are going to use ByteArray here.
var fileRef:FileReference=new FileReference();
var fileFilter:FileFilter=new FileFilter("Images: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg;*.jpg;*.gif;*.png");
var bytArr:ByteArray
fileRef.browse([fileFilter])
fileRef.addEventListener(Event.SELECT,select);
function select(e:Event)
{
fileRef.load()
/*We are going to load selected files data*/
fileRef.addEventListener(Event.COMPLETE,loadComplete);
}
function loadComplete(e:Event)
{
bytArr=e.target.data
/*When load is completed. We will convert it into ByteArray.*/
var ldr:Loader=new Loader()
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,addImage)
ldr.loadBytes(bytArr);
/*As we have now local files data, we can use it as we want. Here I am going to load that ByteArray. As it is an Image I used loadBytes method of loader. If data is text u can directly insert that text into any text box. Same like sound. If local file is sound file you can directly play it once you converted it into Byte Array.*/
}
function addImage(e:Event)
{
addChild(e.target.loader.content)
}
Lets see example how to load image and save same image in local disk.
var fileRef:FileReference=new FileReference();
var fileFil:FileFilter=new FileFilter("Images: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg;*.jpg;*.gif;*.png");
var bytArr:ByteArray
var saveFil:FileReference=new FileReference()
/*We took another FileReference to save file.*/
fileRef.browse([fileFil])
fileRef.addEventListener(Event.SELECT,select);
function select(e:Event)
{
fileRef.load()
fileRef.addEventListener(Event.COMPLETE,comp);
}
function comp(e:Event)
{
bytArr=e.target.data
/*When load is completed. We will convert it into ByteArray.*/
saveFil.save(bytArr)
/*FileReference.save() method will ask you location and name of file*/
You can see this article in FFD Magazine March issue.
}
No comments:
Post a Comment