Read and write local files with Flash Player 10
Tuesday, 15 July 2008This text is slightly out of date for the release version of Flash Player 10. The data for local files isn’t available in the OPEN event, and you’ll need to wait for the subsequent COMPLETE event to fire before you can read the data.
I have updated the FlexPad example application and it’s source code, and I’ll update the article here shortly.
One of the features that I’m most looking forward to in Flash Player 10 (codenamed Astro) is local file access. Once the pipe dream of web application developers the world over, the next version of the Flash Player will allow you to read from and write to local files using a simple ActionScript interface.
This is all implemented with a few simple additions to the FileReference class. Reading the content of a file is accomplished with the FileReference.open() method, which can be called once the user has selected a file using the browse dialog. Like most things in the Flash world file loading happens asynchronously, so you’ll have to listen out for the Event.OPEN event. Once the file has loaded, the file content is available as a ByteArray object through the FileReference.data property to do with as you please.
package com.dynamicflash.examples {
public class LocalFileAccessExample {
public function LocalFileAccessExample():void {
var fileRef = new FileReference();
fileRef.addEventListener( Event.SELECT, onFileSelect );
fileRef.addEventListener( Event.OPEN, onFileOpen );
fileRef.browse();
}
private function onFileSelect( event:Event ):void {
var fileRef:FileReference = event.target as FileReference;
fileRef.open();
}
private function onFileOpen( event:Event ):void {
var fileRef:FileReference = event.target as FileReference;
var data:ByteArray = fileRef.data as ByteArray;
}
}
}
Saving data to a local file is as simple as calling the new FileReference.save() method and passing the data you want written to the file and the filename as parameters. As data you can pass either a String or a ByteArray object.
fileRef:FileReference = new FileReference();
fileRef.save( 'Here is some text', 'some.txt');
In order to make sure that nefarious Internet denizens can’t mess with a user’s files without a their knowledge, the Flash Player pops up a native operating system save dialog every time you try to write data to a file.

Consequently the filename you pass to FileReference.save() is little more than a suggestion to the user, and the default directory for the saved file seems to be the last save directory, rather than the directory the file was loaded from. I think this is a decent trade-off between functionality and security, even if it means that saving data to local files requires a little more effort on the user’s part than with traditional desktop applications.
Note: There is currently a bug in the latest beta of Flash Player 10 where any attempt to overwrite an existing file will result in that file being truncated to zero bytes, rather than being filled with the data you specified. This is a known issue, and you can track the bug over at Adobe’s issue tracker.
To illustrate this new feature, I’ve knocked up a simple application (including source code) that I’m somewhat unimaginatively calling FlexPad for you to play around with.
If you want to build your own projects that make use any of the new features in Flash Player 10, you’ll need to grab a recent stable copy of the Flex SDK. See Targeting Flash Player 10 Beta with Flex SDK 3.0.x for more information.






Nice info, thanks
Alan | Tuesday, 15 July 2008 | 3:03 pmNice info, thanks
Hey, thanks for the new info. But i think that this
Ronny Karam | Tuesday, 15 July 2008 | 3:09 pmHey,
thanks for the new info. But i think that this will force adobe to increase security on the player. And later on we’ll have anti-virus applications deleting our swf files
flexipad doesnt work with flash player 9 too bad. agreed
Jairus | Wednesday, 08 October 2008 | 6:33 amflexipad doesnt work with flash player 9 too bad. agreed with Ronny on the security.
Sound good in theory, Though if a flash app can read
Shafir Ahmad | Tuesday, 21 October 2008 | 8:04 amSound good in theory,
Though if a flash app can read a local users file system, thats not so cool. So it can read sensitive files from the local and send them to a “nefarious” website. Oh oh
hi I just don't seem to be able to load or
Matthias | Sunday, 26 October 2008 | 12:36 pmhi I just don’t seem to be able to load or save anything with your flexpad ? (flash 10 is installed ) (win xp, firefox 3.0)
Will this work for image files too, or just text?
Andrew | Monday, 27 October 2008 | 7:12 pmWill this work for image files too, or just text?
Matthias: I have updated the FlexPad example files so that
Steve | Tuesday, 28 October 2008 | 3:12 pmMatthias: I have updated the FlexPad example files so that they work with the release version of Flash Player 10. If you’re interested, the difference is that local file data is no longer available in the OPEN event – instead you have to wait until the subsequent COMPLETE event fires in order to get at the data from the file.
Andrew: The FileReference API returns a ByteArray object, so you
Steve | Tuesday, 28 October 2008 | 3:13 pmAndrew: The FileReference API returns a ByteArray object, so you could use this to read/write any kind of file including images.
If a flash app can read a local users file
saji | Monday, 17 November 2008 | 5:35 amIf a flash app can read a local users file system, I feel that on one side this would rise security issues and on the other side it would be really useful for programmers like us. Can anyone tell me if this could solve this perticular requirement?
I have created a downloader in flex, This downloader fetches a list of files to be downloaded from the server. Once the list is retrived, on clicking the file names in the list it will open a save dialog box and start downloading the file. My concern is that, I need to create a downloader, where in, the destination folder is selected by the user (only once) for saving the files and then one by one the files are to be downloaded to that folder. Is this possible? Thanks for any help.
i have a source code for image preview, rotate and
Ady Levy | Sunday, 07 June 2009 | 9:36 pmi have a source code for image preview, rotate and upload as jpg after resizing the image.
check out Client side image resize
Ady.
Hi, i'm using Flex builder 3 on windows. I tried running ur
ChandruBS | Monday, 29 June 2009 | 6:24 amHi, i’m using Flex builder 3 on windows. I tried running ur code. fileRef.browse(); works and i get a browse dialog. But … fileRef.open(); doesnt work it gives the following error during compile. 1061: call to a possibly undefined method open through a reference with static type flash.net:FileRefernce
even var data:ByteArray = fileRef.data as ByteArray; gives the following error. “access of undefined property fileRef.”
Please tel me wat cud be wrong… mail id : chandru dot bs4 at gmail dot com
Those worrying about security--for reading local files, the user is
Kaolin Fire | Wednesday, 05 August 2009 | 9:13 pmThose worrying about security–for reading local files, the user is prompted as to what file they want to allow the flash app to read. So the only real security hole is if the user can be convinced to select a file that they shouldn’t. Same for writing.
Actually, I found that this has to be done a
Brett Adam | Wednesday, 05 August 2009 | 9:44 pmActually, I found that this has to be done a little differently in the final Flashplayer 10 version.
Here’s my code
// we need to keep this ref to prevent the fileRef getting GC'd // cause the docs say that if it goes out of scope, you won't get the // completion eventspublic function handleReadFileContent(event:Event):void { fileRef = new FileReference(); fileRef.addEventListener( Event.SELECT, onFileSelect ); fileRef.addEventListener( Event.COMPLETE, onFileComplete ); fileRef.browse(); }
private function onFileSelect( event:Event ):void { fileRef.load(); }
private function onFileComplete( event:Event ):void { var content:ByteArray = fileRef.data as ByteArray;
}
[...] I liked these two blog posts about the filereference
receptionist friendly flash 10 xml editor « headwinds lab | Wednesday, 21 October 2009 | 5:24 pm[...] I liked these two blog posts about the filereference features and the known save bug: deleteaso.com dynamicflash.com [...]