Flash 10 File Reference

Prior to Flash Player 10 , if you need to write or read a file to a user system , you would need to bounce it off a server and then load it back to the user system before you can even access the file. This is no longer true in Flash Player 10 . The new API on FileReference class allows you to save and load file that is selected by the user , they are

FileReference.load() – loads data from a file that is selected by the user

FileReference.save() – save data to a file that is selected by the user

However , in order for the save() and load() to be called it need to be in response to a user interaction such as a button click and the location of the files cannot be exposed to Actionscript.

Below are the examples to use this new API

import mx.controls.Button;
import mx.controls.TextArea;
import flash.net.FileReference;
import flash.events.Event;
import flash.utils.ByteArray;
private var fileReference:FileReference;
private var textArea:TextArea = new TextArea();
private var loadButton:Button = new Button();
private var saveButton:Button = new Button();
private var myFilter:FileFilter = new FileFilter("Text","*.txt");
private function init():void{
	textArea.width = 200;
	textArea.height = 200;
	saveButton.label = "Save"
	loadButton.label = "Upload Text File"
private function saveFile(e:MouseEvent):void{
	fileReference = new FileReference();
private function loadFile(e:MouseEvent):void{
	fileReference = new FileReference();
private function selectFile(e:Event):void{
private function loadText(e:Event):void{
	var data:ByteArray = fileReference.data;
	textArea.text = data.readUTFBytes(data.bytesAvailable);
	fileReference = null;

Of course you can also use Event Listeners to make sure that the file are properly loaded or if the user canceled the file browsing.

Here’s the working example

View Source

Be Sociable, Share!