Calendar

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Tag Cloud

Categories

Archives

Highest Rated

Most Viewed

Recent Posts

Recent Comments


« Could it be? Flash on iPhone | Main | Duane Nickull discusses Forensic Architecture »

FileReference.save() in Flash Player 10

By Rich Tretola | October 1, 2008Print This Post Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
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.

  1. private function snapPic():void{
  2.     if(myMessageTxt.text.length > 0){
  3.         var bitmapData:BitmapData = new BitmapData(pic.width, pic.height);
  4.        bitmapData.draw(pic,new Matrix());
  5.        var bitmap : Bitmap = new Bitmap(bitmapData);
  6.         var jpg:JPEGEncoder = new JPEGEncoder();
  7.         var ba:ByteArray = jpg.encode(bitmapData);
  8.         file.save(ba,myMessageTxt.text + '.jpg');
  9.     } else {
  10.         Alert.show("Please enter your First name","Error");
  11.     }
  12. }

Pretty cool huh. :-)

Share/Save/Bookmark

Topics: Flash Player, Flex, Flex 3 (Moxie) |

5 Responses to “FileReference.save() in Flash Player 10”

  1. JTtheGeek Says:
    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:

    Yep, it is pretty sweet and certainly long overdue.

  2. Jeff Says:
    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:

    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.

  3. kukoc Says:
    November 3rd, 2008 at 10:41 am

    this was awailible in cs3 - nothing new :)
    regards

Comments