Actionscript:
-
var file:FileReference = new FileReference();
-
-
stage.addEventListener(MouseEvent.CLICK, onClick);
-
-
function onClick(evt:MouseEvent):void {
-
file.save("some text. \nsome more text", "actionsnippet.txt");
-
}
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.
6 Comments
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
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!
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…
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
THANK YOU! I HAVE LOOKED EVERYWHERE FOR A WORKING, STRAIGHT FORWARD EXAMPLE OF THIS FOR HOURS, AND THIS IS IT! FINALLY!
glad you found what you were looking for