flash write ByteArray to file on the disk

5.6k views Asked by At

I need to write a ByteArray to a file on local disk with Flash AS3. The flashapplication is run locally (it's a projector exe).

I found the FileReference class with the save() function which works perfect. The only problem is, that this function opens a filebrowser and let's the user select where to store the file. However - i have the path already as string and need to save to this location without useraction (since i'm exporting a lot of files into this directory in one go and don't want the user to choose each one manually).

Is there a way to store a bytearray from a projector to local disk without opening a filebrowser?

I'm also using mdm Zinc, which actually provides a function to save a ByteArray to disk, but this function is for some unknown reasons not working. I already filed a bugreport, but I need to get this to work very urgently, so i'm looking for alternatives!

Thanks!

2

There are 2 answers

2
Mihai Nita On

If all you need is persistence (you read what you write), you can take a look at SharedObject.

0
glastonbridge On

Noting that you are running your application as an .exe I assume you are using some variant of AIR, and have a security permission to access the filesystem. You should take a look at the FileStream class. Here is a very simple example of how I used it to write a ByteArray to a hard-coded file location.

var fs : FileStream = new FileStream();
var targetFile : File = File.desktopDirectory.resolvePath("test.raw");
fs.open(targetFile, FileMode.WRITE);
fs.writeBytes(myByteArray,0,myByteArray.length);
fs.close();