Search

 

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

Tags

Archives


« | Main | »

FileReference.save() in Flash Player 10

By Rich Tretola | October 1, 2008
151,658 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. :-)

Topics: Flash Player, Flex, Flex 3 (Moxie) | 95 Comments »

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

  1. santiago Says:
    February 9th, 2011 at 3:04 pm

    I’m trying to use this example in Flex Builder 3, but I get an error when building my project.

    on the file.save() line I get the following message:
    1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference.

    please let me know if I’m missing something

    thanks!

    Reply to this comment

    Martin Reply:

    Hi friend you should update the compiler version of your flex. In your project properties->flex compiler->required flash player version… just write 10 or whatever you have

    Reply to this comment

  2. Mithula Says:
    March 8th, 2011 at 4:08 am

    I am alos getting the same error message .What will be the reason?

    Reply to this comment

    Chris Reply:

    Hey Mithula, see above. The target Flash Player version for the project is simply too low. You’ll have to target Flash Player 10. I did this and got around the error.

    Reply to this comment

    hector Reply:

    for me still its showing the same error even after changed the property.can u pls help me

    Reply to this comment

  3. Andre Says:
    May 24th, 2011 at 12:23 pm

    Hello there, I´m trying to use something similar, below the functions, when I try to save the local xml is not saving the changes that the user make on function “texto_painel_change”, I really dont know how to transfer the content typed in the function “item_click” to the function “salvar_xml_click”, if you please could give a hand:

    salvar_xml.addEventListener(MouseEvent.MOUSE_DOWN, salvar_xml_click)
    function salvar_xml_click(e:MouseEvent):void
    {
    var file:FileReference = new FileReference();
    file.save(xml,”.xml”);
    texto_painel.htmlText;
    FileFilter(“Documents”,”*.xml”);
    }

    myGrid.addEventListener(MouseEvent.CLICK, item_click)
    function item_click(e:Event):void
    {
    texto_painel.htmlText = myGrid.selectedItem.label;
    }

    texto_painel.addEventListener(Event.CHANGE, texto_painel_change)
    function texto_painel_change(e:Event):void
    {
    myGrid.selectedItem.label = texto_painel.htmlText;
    }

    Reply to this comment

  4. Marcello Rego Says:
    May 25th, 2011 at 2:54 pm

    The method “save” in FileReference is only available using PlayerGlobal.swc version 10. In other words, you may compile your Flex app using FlashPlayer 10 as minimum requirement.

    Reply to this comment

  5. Andre Says:
    May 26th, 2011 at 4:06 pm

    Yes, Im compiling in Flash IDE, everytyhing is working my nly problem is to save the changes from the DataGrid to the local XML. But thank you for your reply.

    Reply to this comment

  6. john Says:
    June 8th, 2011 at 12:09 am

    Does it work only in FLEX? Or it will work with what version of Flash? (Flash cs4,cs5?)

    Thanks.

    Reply to this comment

    Rich Tretola Reply:

    Flex as well.

    Reply to this comment

    parth Reply:

    How to add a as3 file to flash library…………

    Reply to this comment

  7. slb1904 Says:
    June 24th, 2011 at 7:50 am

    Hi,

    I have a very similar code but in Flash CS4 using AS3.

    My file.save code works perfectly well when I test the swf within flash, however, once I try it on a browser (whether it’s local or online) it fails to bring up a dialog box where I can save the image.

    Is there something specific required for it to work on a browser online? Am I missing something?

    Thanks for any replies.

    Regards,

    SLB

    Reply to this comment

    Rich Tretola Reply:

    Not sure how you are triggering the save method. Flash security in the browser says that the FileReference.save() method can only be called based on user interaction (like clicking on a button). You can not call FileReference.save() programmatically. Run the debug player in the browser and you may see a security warning if this is what your application is doing.

    Reply to this comment

  8. slb1904 Says:
    June 24th, 2011 at 2:17 pm

    Thanks Rich! Your advise helped.

    I was aware that it must be called during a user interaction, however I didn’t know it wouldn’t work with an embedded function, i.e. MouseEvent function calls function downloadImage(myImage), where the save method was being invoked.

    I moved the save method in the MouseEvent function and the dialog box now opens when the swf is loaded from a browser! I just need to reshuffle my code now :)

    Many thanks

    Reply to this comment

  9. Paul Says:
    July 6th, 2011 at 1:01 pm

    Thanks for the article, is there any way to get around the user interaction requirement? For our application, a button click leads to a call to an HTTP Request, the result of which I want to put in a file. So my fileRef.save() method is currently in my response handler and doesn’t do anything.

    I’m trying to think of a way around this – any ideas?

    Reply to this comment

    Rich Tretola Reply:

    No, there is no way around the user interaction requirement. This was a change with Flash Player 10. Here is the article from 2008.

    http://blog.everythingflex.com/2008/10/17/filereferencebrowse-bit-me-on-the-ass-today/

    Reply to this comment

    Paul Reply:

    ah, ok, thanks! Looks like I will have to do a two button deal, first to fetch and second to actually do it.

    Reply to this comment

  10. parth Says:
    July 19th, 2011 at 5:36 am

    how to add jpgencoder to flash library..

    user:naive

    Reply to this comment

  11. Noel Drew Says:
    October 20th, 2011 at 7:51 am

    Hi Rich,

    I’ve had great success saving images and text/xml to my local system but am a little stuck with my most recent project. I want to be able to drag an .mp4 file into my flex app and, on button click, save the .mp4 to a hardcoded destination in the application folder. I have built this using the AIR runtime but can’t seem to get it to work. Is this even possible?

    Reply to this comment

    Rich Tretola Reply:

    So you want to be able to drag an .mp4 file from your desktop only an AIR application and then move the file to a specific folder?

    Reply to this comment

Comments