FileReference.save()

Actionscript:
  1. var file:FileReference = new FileReference();
  2.  
  3. stage.addEventListener(MouseEvent.CLICK, onClick);
  4.  
  5. function onClick(evt:MouseEvent):void {
  6.     file.save("some text. \nsome more text", "actionsnippet.txt");
  7. }

This is possibly my favorite feature of flash 10. Save any kind of file to the users computer...

The first argument is for the data to put in the file, this can be a String, ByteArray or XML object. The second argument is the name of the file.

As a test I also created one that saved a BitmapData object as a jpeg. But it doesn't really fall into the category of snippet because it uses the Jpeg Encoder from adobe.

This entry was posted in misc and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Bubba
    Posted September 3, 2009 at 8:53 am | Permalink

    Hi there.. you say “I also created one that saved a BitmapData object as a jpeg”.. do you have the code that you can post or email to me for that? I tried out this code above for fileReference.save() and it worked out awesome, thanks so much! I would appreciate the bitmap data code too. I am very very poor at actionscript, so I need code to be able to understand what’s going on. Thanks

  2. Bubba
    Posted September 3, 2009 at 9:02 am | Permalink

    Also, how do you add more text each time as a new choice is made? For example, I want the person to be able to click on several choices in a form, and then hit submit, and it will save each choice in a text file. And then go to the next page, and do more questions by clicking, and it will add on to that same file. How should I modify the code for that? THanks!

  3. Posted September 4, 2009 at 10:19 am | Permalink

    Here is how you save a jpeg:

    http://actionsnippet.com/?p=1093

    and if I recall correctly … the text will just be added to the end of the file by default, so you simply need to save with the same filename - however it sounds like you should look into using something called “shared objects” instead…. try googling that you’ll see what I mean…

  4. Nayan Nilank
    Posted May 20, 2010 at 11:09 pm | Permalink

    Hi,

    I am trying to use the same function in SDK 3.5. But I am getting an error that “Unable to make a call to undefined method”. Did anyone face that?

    Thanks,
    Nayan

  5. Carl
    Posted February 17, 2011 at 6:09 pm | Permalink

    THANK YOU! I HAVE LOOKED EVERYWHERE FOR A WORKING, STRAIGHT FORWARD EXAMPLE OF THIS FOR HOURS, AND THIS IS IT! FINALLY!

  6. Posted February 17, 2011 at 9:38 pm | Permalink

    glad you found what you were looking for :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*