views:

583

answers:

2

If I'm generating a binary file inside Flash Player, how do I pop up a 'Save' dialog for the file without sending it up the the server first?

I'm sure I heard this was a new feature in Flash 10.

Cheers,
James

A: 

If you are inside the flash player, you can do the following:

import flash.net.FileReference;
import flash.utils.ByteArray;

var fileReference:FileReference = new FileReference();

var data:ByteArray; // load up your data into this ByteArray

fileReference.save(data, "outputfile.dat");

This will not work if you are running in the browser-- at least as far as I remember, but if you are in the flash player this will work.

The FileReference.save() method will popup a dialog for the user to choose where to save the file, so the filename given is just a suggested filename.

Edit: See Technomaag's comment below also if you're having problems

Kekoa
I believe you can't do this anymore with the lastest Flash Player
PeZ
@Pez: Where do you read this? It still works for me. It may not work in the browser, but in the flash player it works.
Kekoa
In Flash Player 10 I think you need to call fileRef.save() in response to either key or mouse press. I.e. there needs to be user interaction triggering this method for it to work.This is a security feature and change of functionality from FP9 to FP10.
Tehnomaag
@Tehnomaag: Good info, I've never used without user interaction, so that part had alluded me.
Kekoa
+1  A: 

This will help, and really show you how to do it, all you have to keep in mind is that opening and saving files can only be done with user initiation, it cannot be automated, so not sure what you are trying to do, but this will help:

http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/

joseeight
The example code there is Flex-based, but main part of the inline script there is easy to reuse in plain AS3 project as well.
Tehnomaag