How can you save String to a file in AS3?

I have a line that the user typed, and I want to save it to a file on the user's hard drive. Can you do it? And if so, how?

+6
flex flash actionscript-3 filereference
source share
4 answers

Yes, you can using FileReference. This is mainly done:

var bytes:ByteArray = new ByteArray(); var fileRef:FileReference=new FileReference(); fileRef.save("fileContent", "fileName"); 

Isn't it too hard to look like, is it? And there is also a video tutorial here:

http://www.gotoandlearn.com/play?id=76

And the documentation:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/

Hope this helps.

+9
source share

Since I had a function to output bytes to a file (because I was doing something with bitmaps), I used it again to output a string, for example:

  var filename: String = "/Users/me/path/to/file.txt";
 var byteArray: ByteArray = new ByteArray ();
 byteArray.writeUTFBytes (someString);
 outFile (filename, byteArray);

 private static function outFile (fileName: String, data: ByteArray): void {
     var outFile: File = File.desktopDirectory;  // dest folder is desktop
     outFile = outFile.resolvePath (fileName);  // name of file to write
     var outStream: FileStream = new FileStream ();
     // open output file stream in WRITE mode
     outStream.open (outFile, FileMode.WRITE);
     // write out the file
     outStream.writeBytes (data, 0, data.length);
     // close it
     outStream.close ();
 }
+5
source share

In addition, Flash Player 10 and the Flex Gumbo SDK must be installed on your Flex Builder 3.

You can also see the following example: http://blog.flexexamples.com/2008/08/25/saving-files-locally-using-the-filereference-classs-save-method-in-flash-player-10/

+3
source share

In Flex 3, no, you cannot do this unless you upload the file to the server, and then upload the file using the URL to the desktop.

In Air or Flex 4, you can save it directly from the application to the desktop, as described above.

+1
source share

All Articles