Calendar

October 2008
S M T W T F S
« Sep   Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Tag Cloud

Categories

Archives

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
13,372 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
2
3
4
5
6
7
8
9
10
11
12
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. :-)

Share this Post


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

42 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.

    everythingflex

    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

    everythingflex

    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

    everythingflex

    Rich Tretola Reply:

    No, it was not available in cs3. There was no save to disk functionality native to the Flash Player before 10.

  4. YosiDov Says:
    December 7th, 2008 at 9:40 am

    why gives an error 1061…?

    yes, I have player 10….

    :-(

  5. Gary Says:
    December 12th, 2008 at 12:56 pm

    From now MS ought to implement the same feature in Silverlight. And it would be cool have the possibility to save not only complete byteArray, but also some kind of stream ;-)

  6. MikeZ Says:
    January 5th, 2009 at 8:40 pm

    Hi, I get an error on line 16 when I try and launch. Apprently there is something wrong with:

    file.save(ba,myMessageTxt.text + ‘.jpg’);

    Although looking at it the code the error makes no sense :P

    everythingflex

    Rich Tretola Reply:

    What did you type in for the name? It could be some invalid characters that I am not stripping.

    MikeZ

    MikeZ Reply:

    Named the new project SaveToDisk, everything opened correctly, (made sure all the correct files are in place by copying over everything but not replaceing whats already there) but I got the same error as the guy 2 posts above me when I try and launch SaveToDisk:

    1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference. SaveToDisk/src SaveToDisk.mxml line 16

    everythingflex

    Rich Tretola Reply:

    Sorry, I thought you were getting the error using my online sample. If you are building it yourself, you need to makes sure you are compiling with SDK 3.2 as a minimum and running Flash Player 10.

    MikeZ

    MikeZ Reply:

    Hi, I was getting the error with your online sample, sorry to sound confusing :P

    everythingflex

    Rich Tretola Reply:

    Please let me know what version and point release of the Flash Player you are using. Right click and choose About Flash Player from the context menu.

    MikeZ

    MikeZ Reply:

    I might ask you question quickly while I have you here (I’m Australian so sorry about the inconvenience of the time difference!) I was just wondering if a version of the code that saves a completed swf (like colouring in a picture) be possible with the filereference.save () function or is saving a still of a movie as an image to complex for a developer (or flex) to calculate? If you dont want to answer thats cool :)

  7. JTTheGeek Says:
    January 5th, 2009 at 11:38 pm

    MikeZ, being that swf is now an open format, it is theoretically possible to create a runtime flash compile built in flash. I’m guessing this is probably easier now that alchemy is out. This would be quite an undertaking though. Regarding save an image out, well thats very easy. You can render any movie clip to an bitmap in as3 no problem, and there are many ways to encode that as a jpg or png. Saving a movie out however is quite a bit more intense and likely not an ideal solution, but definitely within the real of possible. Great questions, I’m excited to be able to even say these are possible in flash.

  8. matt b Says:
    January 28th, 2009 at 2:11 pm

    Can this be done in Flash CS3 rather than through Flex?
    I’m getting the following error…

    1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference.

    everythingflex

    Rich Tretola Reply:

    Yes, just make sure you are compiling for Flash Player 10.

    matt b

    matt b Reply:

    Cool, so I’ll need CS4 then, or is there a way to publish to player 10 in CS3?

    Tom

    Tom Reply:

    I’m doing this in CS4 but the flash app is running as a projector from a DVD. I want users to be able to save a number of different file formats - JPEG, MOV etc. - from the same DVD. Is this possible with the filereference.save function?

    Thanks

  9. Kalen Gibbons Says:
    January 30th, 2009 at 5:53 pm

    If in Flex you are receiving the following error:

    1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference

    Go to Project Properties >> Flex Compiler, and set “Required Flash Player Version” to 10. That should clear it up.

  10. Thangamariappan G Says:
    February 10th, 2009 at 10:11 am

    Awesome work done! I appreciate you putting it in online to help many people like me.
    Happy posting
    Thanks
    thangamariappan

  11. jm Says:
    February 13th, 2009 at 1:44 pm

    i am also getting this error in compile

    1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference

    i am compiling with ant. any ideas ???

  12. mookstar Says:
    February 25th, 2009 at 3:46 am

    I’ve been waiting for this and I’d given up hope … i wonder what percentage of users are on Flash Player 10…

    everythingflex

    Rich Tretola Reply:

    FP 10 is already over 55% percent and growing very very fast.

  13. post Says:
    February 25th, 2009 at 6:52 pm

    One of the best Meupload file searchers and download centres is here http://megauploadfiles.com/
    Find al the necessary information there!

  14. Solerous Says:
    March 4th, 2009 at 9:48 am

    Thanks for putting this out and thanks for the explanation on how to get it to work via the Project Properties. For some reason though, even that doesn’t work for me. I was using Flash Player 10.0.12.36. So I tried the new 10.0.22.87 and that didn’t work either. I even tried converting to an AIR app and couldn’t get it to work. I am using your code and just can’t get rid of the 1016 error.

    everythingflex

    Rich Tretola Reply:

    What SDK are you compiling against?

  15. YosiDov Says:
    March 17th, 2009 at 6:11 am

    It is a way to open a file with player 10 different than fscommand exec?

  16. Aend Says:
    March 20th, 2009 at 4:24 am

    This is a nice feature, but what I really need is to do this programmatically, without user interaction, like in AIR.
    The only solution for browser based local storage are LSO’s. Unfortunately LSO’s are terribly slow while storing large files (10 > MB).

    everythingflex

    Rich Tretola Reply:

    That used to be possible, but was removed due sto security concerns, click jacking. See http://blog.everythingflex.com/2008/10/17/filereferencebrowse-bit-me-on-the-ass-today/

  17. vivek Says:
    May 15th, 2009 at 6:45 am

    Hello
    I need the same function in AS 2.0
    can you plz help me .
    Thanks in Advance

    everythingflex

    Rich Tretola Reply:

    Sorry, but this is not possible in AS2.

  18. Rubbernecker’s Review - Week 15 | Learn Flex Says:
    May 17th, 2009 at 3:44 am

    [...] FileReference.save() in Flash Player 10 (from EverythingFlex) [...]

  19. todd Says:
    May 23rd, 2009 at 3:23 pm

    Hi,
    How could I do this to save the stage instead of a single movie clip? My users enter some info etc. on the stage and would need to download the entire contents as a jpg to email.

    Please tell me I can….

  20. LaaC Says:
    May 24th, 2009 at 12:43 pm

    wow… Thats cool… but… It can’t be implemented to flash :( I need it in flash, not flex T.T

    FileReference.save() is not available on flash. are there any flash version of this example?

  21. todd Says:
    May 24th, 2009 at 12:47 pm

    I believe it is if you are publishing for flash player 10 and using cs4.
    Is that what you are using?

  22. Rich Tretola Says:
    May 24th, 2009 at 7:39 pm

    Make sure you are compiling for Flash Player 10.

  23. todd Says:
    May 26th, 2009 at 6:14 am

    Hi,
    Can you tell me how I could save instead of a movieclip, a certain area of the stage?

    everythingflex

    Rich Tretola Reply:

    The sample code listed in this article shows the saving of the component with the id pic. pic can be any display object. Just use the code shown above and substitute the id of the movieclip you wish to save in place of the word pic.

    Eric Hainey

    Eric Hainey Reply:

    I use this to save an area within my Flex application. It requires you to pass the object that you want to save the image of.

    private function takeSnapshot(source:IBitmapDrawable, width:int, height:int):void{
    var jpgEnc:JPEGEncoder = new JPEGEncoder(100);
    var bitmapData:BitmapData = new BitmapData(width, height);
    bitmapData.draw(source);
    var bmp:Bitmap = new Bitmap(bitmapData);

    var jpgSnap:ImageSnapshot;
    jpgSnap = ImageSnapshot.captureImage(bmp, 0, jpgEnc);

    var jpgData:String = ImageSnapshot.encodeImageAsBase64(jpgSnap);

    fscommand(’jpeg’, jpgData);
    }

  24. todd Says:
    May 26th, 2009 at 6:15 am

    using flash

Comments