« Could it be? Flash on iPhone | Main | Duane Nickull discusses Forensic Architecture »
FileReference.save() in Flash Player 10
| By Rich Tretola | October 1, 2008 | |
| 30,361 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) | 74 Comments »









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.
Reply to this comment
Rich Tretola Reply:
October 9th, 2008 at 7:18 am
Yep, it is pretty sweet and certainly long overdue.
Reply to this comment
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
Reply to this comment
Rich Tretola Reply:
October 9th, 2008 at 12:58 pm
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.
Reply to this comment
November 3rd, 2008 at 10:41 am
this was awailible in cs3 – nothing new
regards
Reply to this comment
Rich Tretola Reply:
January 6th, 2009 at 12:26 am
No, it was not available in cs3. There was no save to disk functionality native to the Flash Player before 10.
Reply to this comment
December 7th, 2008 at 9:40 am
why gives an error 1061…?
yes, I have player 10….
Reply to this comment
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
Reply to this comment
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
Reply to this comment
Rich Tretola Reply:
January 5th, 2009 at 9:21 pm
What did you type in for the name? It could be some invalid characters that I am not stripping.
Reply to this comment
MikeZ Reply:
January 5th, 2009 at 10:14 pm
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
Reply to this comment
Rich Tretola Reply:
January 6th, 2009 at 12:25 am
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 Reply:
January 6th, 2009 at 1:16 am
Hi, I was getting the error with your online sample, sorry to sound confusing
Rich Tretola Reply:
January 6th, 2009 at 2:22 pm
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 Reply:
January 5th, 2009 at 10:35 pm
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
Reply to this comment
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.
Reply to this comment
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.
Reply to this comment
Rich Tretola Reply:
January 28th, 2009 at 2:45 pm
Yes, just make sure you are compiling for Flash Player 10.
Reply to this comment
matt b Reply:
January 28th, 2009 at 2:49 pm
Cool, so I’ll need CS4 then, or is there a way to publish to player 10 in CS3?
Reply to this comment
Tom Reply:
May 12th, 2009 at 2:28 pm
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
Reply to this comment
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.
Reply to this comment
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
Reply to this comment
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 ???
Reply to this comment
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…
Reply to this comment
Rich Tretola Reply:
February 25th, 2009 at 5:44 am
FP 10 is already over 55% percent and growing very very fast.
Reply to this comment
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!
Reply to this comment
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.
Reply to this comment
Rich Tretola Reply:
March 4th, 2009 at 10:20 am
What SDK are you compiling against?
Reply to this comment
March 17th, 2009 at 6:11 am
It is a way to open a file with player 10 different than fscommand exec?
Reply to this comment
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).
Reply to this comment
Rich Tretola Reply:
March 20th, 2009 at 4:53 am
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/
Reply to this comment
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
Reply to this comment
Rich Tretola Reply:
May 15th, 2009 at 7:21 am
Sorry, but this is not possible in AS2.
Reply to this comment
May 17th, 2009 at 3:44 am
[...] FileReference.save() in Flash Player 10 (from EverythingFlex) [...]
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….
Reply to this comment
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?
Reply to this comment
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?
Reply to this comment
May 24th, 2009 at 7:39 pm
Make sure you are compiling for Flash Player 10.
Reply to this comment
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?
Reply to this comment
Rich Tretola Reply:
May 26th, 2009 at 6:54 am
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.
Reply to this comment
Eric Hainey Reply:
July 2nd, 2009 at 12:28 pm
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);
}
Reply to this comment
May 26th, 2009 at 6:15 am
using flash
Reply to this comment
August 11th, 2009 at 3:41 pm
And without PHP. Man, this is cool. Thanks
Reply to this comment
September 2nd, 2009 at 7:03 pm
Having had the same trouble as many of the posters here’s what I found:
- YES this does work with flash (i.e. from browser not in AIR)
- YES this does work with CS3
- YES you do get an error in flash UNLESS you compile
specifically for flashplayer 10
- In Flex Builder you can use Project Properties >> Flex Compiler, and set “Required Flash Player Version” to 10.0.00
- if you use the SDK you can either:
> add the option “-target-player=10.0.00″ to the mxmlc options line OR:
> modify the flex_sdk_3/frameworks/flex-config.xml file to change that same option
Note that the compiler option trumps the config file.
then… you may get another error: in flash this function is only allowed for an interactively initiated function. In other words if you have this save() as part of a file completion handler it will not work since it is called from a non interactively invoked method. It will work from, for example, a mouseclick handler.
Reply to this comment
September 15th, 2009 at 8:35 pm
Can any one guide me how to use these file (in zip) in Flash CS4.
How i load/open them in Flash CS4?
Regards.
Reply to this comment
September 21st, 2009 at 5:39 pm
I compiled the project in Flex Builder 3 using Flex SDK 3 and ticked the option for “Require Flash Player version – 10.0.00″.
I am still getting the error on Line 16 with the line:
file.save(ba,myMessageTxt.text + ‘.jpg’);
Can you please let me know what else I need to do in order to make it compile.
Cheers.
Reply to this comment
Rich Tretola Reply:
September 22nd, 2009 at 4:24 am
What is the error?
Reply to this comment
Anthony Reply:
September 22nd, 2009 at 8:36 am
Hi Rich,
The error I am getting is:
“1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference” on line 16 of the code.
It’s the same error some other people have reported earlier on this blog.
I am compiling with Flex 3 SDK in Flex Builder 3 and I have checked the box which indicates it is required for Flash 10.
Is there anything else that I need?
Thanks for your help.
Reply to this comment
Rich Tretola Reply:
September 22nd, 2009 at 8:45 am
I believe you need SDK 3.3 as a minimum.
Anthony Reply:
September 22nd, 2009 at 8:53 am
Thanks for your fast response.
I download Flex SDK 3.4 but I’m not sure how to install it.
The release notes are not clear on the installation process.
Would you be able to tell me how I use the new SDK?
Thanks.
Anthony Reply:
September 22nd, 2009 at 8:58 am
It works once I use Flex SDK 3.4.
Thanks!!!
September 28th, 2009 at 9:08 pm
A lot of my customer base still uses Flash 9. Was trying to determine that if Flash 10 was loaded I would conditionally provide a feature in my to save. I tried faking out the compiler by doing something like this.
if (isFlash10) {
var f:Object = new FileReferende()
f.save(”xyz”,null)
}
but I get an error. Everything I found leads me to believe that I need to compile for Flash 10. However, I fear that will mess up my Flash 9 users.
Does anyone have any advice?
Reply to this comment
Rich Tretola Reply:
September 29th, 2009 at 7:04 am
If you can’t force the users to upgrade, you would need to do the test for Flash 10 within the html wrapper file and then include the version of the SWF that is appropriate.
Reply to this comment
October 30th, 2009 at 9:05 am
hi All, In flex 3 web application how to set default save location for images?
Thanks.
Reply to this comment
November 24th, 2009 at 4:27 am
Flash CS4 Prof, Windows: even if publish for Player 10, it doesn’t work since Flash can’t find the graphics package. I searched for it and for the class pngencoder.as but couldn’t find one. Need I have to install a Flex sdk and then try Flash again? Can I elsewhere download the appropriate classes?
Reply to this comment
November 25th, 2009 at 11:08 am
Hi,
Thanks for the help
i am using flex library
i get same error as before for the library (1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference.)
but not for a “normal” project .
the problem in the eclipse flex library project, there are no property that define the flash version (need to work for 10)
any ideas?
thanks,
ziv
Reply to this comment
November 25th, 2009 at 11:13 am
ok just found great explanation:
http://opensource.adobe.com/wiki/display/flexsdk/Targeting+Flash+Player+10
thanks
Reply to this comment
December 16th, 2009 at 11:39 am
Is this possible to make in Swishmax 3?
Reply to this comment
December 16th, 2009 at 3:36 pm
this is a cool feature it was long due now you will not have a limitation to save the file reference on a local system
Reply to this comment
December 26th, 2009 at 1:48 pm
Awesome work done! I appreciate you putting it in online to help many people like me.
Happy posting
Thanks
Reply to this comment
January 1st, 2010 at 2:51 pm
Thanks for this post – I really like it!!
Reply to this comment
January 4th, 2010 at 9:37 am
Thanks for the info and post.
Does Adobe Flex work with Flash Player 10?
Reply to this comment
January 6th, 2010 at 3:33 am
5 Would you like the discount Mbt shoes? Mbt lami shoes are up to 85% off and Mbt m walk up to 81% off . http://www.mbt-lami.com
Reply to this comment
January 17th, 2010 at 10:58 am
Awesome work done! if know more registry cleaner for windows 7 information,here:http://www.regtweaker.com
Reply to this comment
February 7th, 2010 at 8:16 am
hi All, In flex 3 web application how to set default save location for images?
Thanks.
Reply to this comment
February 8th, 2010 at 7:59 pm
Nice work. Keep up with the great articles about Flash Player.
Reply to this comment
February 8th, 2010 at 8:00 pm
Good article. Looking forward to more of them in the future.
Reply to this comment
February 12th, 2010 at 8:20 pm
I tried it, and still get an error.
Reply to this comment
February 27th, 2010 at 1:47 am
Halli Hallo Hallöle … Google gerade im Web und bin auf dieser schönen und informativen Homepagegelandet. Bevor ich weiter surfe, möchte ich noch einen Gruß von der wunderschönen Insel Sylt hinterlassen.
Reply to this comment
March 3rd, 2010 at 10:50 pm
wow,it is really great, i will try it later.
Reply to this comment
March 3rd, 2010 at 10:52 pm
so wonderful.very detail.
Reply to this comment
March 3rd, 2010 at 10:53 pm
great,thank you for your share
Reply to this comment
March 8th, 2010 at 4:49 am
百度
[url=http://www.sina.com]sina[/url]
Reply to this comment
March 8th, 2010 at 4:51 am
mbt shoes sale
mbt sale
mbt
runescape gold
Reply to this comment