Dynamic Flash

Confessions of a serial code abuser
  • rss
  • Home
  • MTASC
  • Archives
  • About me
  • Goodies
    • Base64 encoder/decoder class
  • My Bookshelf
  • My Talks

Read and write local files with Flash Player 10

Tuesday, 15 July 2008

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.

Astro's file save dialog

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.

Comments
3 Comments »
Categories
ActionScript, Flash, Flex 3
Tags
actionscript3, astro, Flash, flex, tutorial
Comments rss Comments rss
Trackback Trackback

About Dynamic Flash

Steve Webster is a Senior Web Developer for Yahoo! in London, UK.

He is more than a little concerned that he defines himself in terms of his career, and that he talks about himself in the third person.

Find out more

Recent Posts

  • FotB ‘08 Schedule: iCal feeds
  • Read and write local files with Flash Player 10
  • The problem with SproutCore
  • Yahoo! London seeks Junior Developers
  • I am Singularity

Tags

Accessibility ActionScript actionscript3 actionscript 3.0 air apple astro book calendar conference designer filereference file upload Flash flex fotb08 framework ical internet internet explorer JavaScript jobs junior microsoft opportunities ria schedule singularity08 skin tutorial urlrequest web developer web development Web Standards Yahoo!

del.icio.us-ed

Stuff

Singularity?
Flex.org - The Directory for Flex
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox