« Could it be? Flash on iPhone | Main | Duane Nickull discusses Forensic Architecture »
FileReference.save() in Flash Player 10
| By Rich Tretola | October 1, 2008 | Print This Post
|
| 1,674 views |
One of the cool new features of Flash Player 10 is the FileReference.save() method which allows the Flash Player to write to the local file system as long as the save request is requested by the user. This means that upon calling FileReference.save() the default file system dialog will open.

The FileReference.save() method has 2 arguments. The first is the the data which follows the rules listed below:
* If this parameter is null, an exception is thrown.
* If this parameter is a String, it is saved as a UTF-8 text file.
* If this parameter is XML, it is written to a file in XML format, with all formatting preserved.
* If this parameter is a ByteArray, it is written to a data file verbatim.
* If this parameter is none of the above, save() will call toString() on it, and if that fails, an exception is thrown.
The second is the name that the file should be saved as which can be null allowing the user to enter a file name.
In my example, the workhorse is the snapPic() method shown below. This method first gets the BitMapData from the Canvas component and then uses the JPEGEncoder class to create a ByteArray. Finally, the FileReference.save() method is called and the ByteArray and file name are passed in.
To test this application, please click here. *** IT REQUIRES FLASH PLAYER 10 ***
To view the full source, please click here.
- private function snapPic():void{
- if(myMessageTxt.text.length > 0){
- var bitmapData:BitmapData = new BitmapData(pic.width, pic.height);
- bitmapData.draw(pic,new Matrix());
- var bitmap : Bitmap = new Bitmap(bitmapData);
- var jpg:JPEGEncoder = new JPEGEncoder();
- var ba:ByteArray = jpg.encode(bitmapData);
- file.save(ba,myMessageTxt.text + '.jpg');
- } else {
- Alert.show("Please enter your First name","Error");
- }
- }
Pretty cool huh. ![]()
Topics: Flash Player, Flex, Flex 3 (Moxie) |









October 8th, 2008 at 5:09 pm
Wow… as simple as a feature this is, it is a major step forward. I’m giddy at the fact I don’t have to write all file saving methods server side to do things like saving xml data back to the user.
Rich Tretola Reply:
October 9th, 2008 at 7:18 am
Yep, it is pretty sweet and certainly long overdue.
October 9th, 2008 at 12:26 pm
Rich,
The one item that is super cool with AIR is the abiity to save multiple files in sequence, as the result of a single user gesture or programmatically, without additional guestures being requried. It would seem that the new capability falls short of that - correct? It’s not as though a single save dialog would be triggered for all the files you’re trying to download - you’d get one for each filereference, even if the previous directory location is saved.
Jeff
Rich Tretola Reply:
October 9th, 2008 at 12:58 pm
Yes, it is not like AIR, you can only read or write 1 file at a time and each process must be launched by a user interaction.
November 3rd, 2008 at 10:41 am
this was awailible in cs3 - nothing new
regards