Search Flex Components Free

Custom Search

December 10, 2007

FileIO - Central Learns to Read and Write

The single best addition to Central 1.5 is FileIO. With FileIO you can read, write, upload, and download files directly within a Central App.&

To use FileIO you need to use the FileReference Class. Just like MovieClip or Object, FileRefence is a default class within the Central Player. To use it you need to create an instance:

myFile = new FileReference()

Simple, eh! But wait, FileReference instances do not do anything until you intialize the instance to a file. See the Class needs a file to work with and that is the essence of the initialization process. Initailization either invoves binding it to an exisitng file or creating one from scratch.

So lets intialize a file in the Central Cache by creating a file. The first argument is the path to the file and the second is to overwrite a file if it exists.

myFile.create("myFile.txt", true)

Central just created an empty file named "myFile.txt". If the application were installed from "" this file is located here:

C:\Documents and Settings\Theodore Patrick\Application Data\Macromedia\Central\#Central\{MAGICID}\Local Internet\\myFile.txt

(Files on Mac are stored on a similar path)

So we have a file, lets write some text into it!

myFile.writeString("Hello World, I am written with Central!")

Now files within the Central cache are fun but writing the file locally is much better. To do this we use the saveAs method to prompt the user to put the file onto their local file system.


When this executes, the user will recieve a "Save As" dialog box that looks similar to this one:

The last thing we need to do is close the FileReference like so.


TADA, Central can read and write. What is amazing is that FileReferences can read and write binary files which dramatically expands your file capabilites.

I will post some other FileIO goodness soon.

Related Flex Tutorials