Save File in AS3

Opens a dialog box that lets the user save a file to the local filesystem. Although Flash Player has no restriction on the size of files you can upload, download, load or save, the player officially supports sizes of up to 100 MB.

The save() method first opens a dialog box that asks the user to enter a filename and select a location on the local computer to save the file. once Selected and confirms the save operation (for example, by clicking Save), the save process begins. Listeners receive events to indicate the progress, success, or failure of the save operation.

FileReference save method has only to be invoked upon user interaction, for example by a mouse click or button press.


<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” >
<mx:Script>
<![CDATA[
private var file:FileReference = new FileReference();
private function onCreate(e:Event):void
{
var Txt:String=”Hello World”
file.save(Txt,”Sample.txt”); // Initiates the save event and opens up a dialog box for user to enter filename and select a location. Once confirmed, it get saved
} ]]>
</mx:Script>
<mx:Button x=”133″ y=”209″ label=”Create File” click=”onCreate(event)”/>
</mx:Application>

Save File in Adobe Flex -AIR

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”onCreate(event)”>
<mx:Script>
<![CDATA[
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
private
function onCreate(e:Event):
void
{

var file:File = File.desktopDirectory; //Set the Directory location to Desktop
file = file.resolvePath(
“test.txt); //File Name
var
fileStream:FileStream = new
FileStream();   // Create new FileStream to Perform Read or Write
fileStream.open(file, FileMode.WRITE); // Open file in WRITE Mode
fileStream.writeUTFBytes(
“Hello World”); //Write Hello World into test.txt
fileStream.close(); //Close the File Stream
}

]]>
</mx:Script>
</mx:WindowedApplication>


This code creates a file named test.txt with content “Hello World” in desktop folder.

The desktopDirectory property provides a way to reference the desktop directory that works across platforms. If you set a File object to reference the desktop directory using the nativePath or url property, it will only work on the platform for which that path is valid Similarly documentDirectory /applicationStorageDirectory /applicationDirectory can be used to represent the folder.

var file:File = new File();
file = file.resolvePath(
“c:\\test.txt”);

This will work in windows platform and stores file in user mentioned location.