Local File Access API Proposal

Peter Hall
Jan 23rd 2004 (changes)

The API consists of 3 new classes:

Key features of the API

  1. A valid File instance may only be created by the user browsing to the file to load or browsing to a directory and saving data file. So the developer cannot open an arbitrary file or control the location or name of a file to be saved.
  2. The first time a file is opened in a session, it is always guaranteed to be with the user's permission.
  3. Once the File instance is created, the developer has the ability to repeatedly reference it within the session.
  4. A file handle is valid only within the current instance of the Flash Player and may not be usefully stored in a SharedObject or transmitted in a Remoting call or via a LocalConnection.
  5. Gives the developer safe access to file attributes such as date modified
  6. Upload and download to a remote server, while still being limited to the security restrictions above.
  7. Ties in to the proposed Library Management API

If a file is loaded using the API, but then deleted by the user, the file object becomes invalid and the methods save and revert will fail and return false if the file is passed to them.

Different types of data may be stored in a file or retrieved. The type of existing files is determined automatically by Flash and the data property will always be compatible with Flash. If the encoding is "text" then the toString is called on the data before it is saved. If the encoding is "asobject" the data is serialised using the .sol file format.

The encoding is not changeable once a file is created. To change the way that a file is encoded you would have to prompt the user to overwrite it by invoking FileSystem.saveAs() like this:

FileSystem.saveAs(myFile.data, listener, myFile.name, myFile.directory, "asobject");

 

FileSystem class

An object that manages loading, saving, uploading and downloading of files.

Constructor

new FileSystem();

Creates a new FileSystem instance.

Parameters:

None.

Returns

Nothing.

Events

onStatus

FileSystem.onStatus = function(){

}

Parameters:

None.

Returns

Nothing.

onOpenFile

 

onSaveFile

 

Methods

saveAs

public function saveAs(data:Object [,suggestedName:String [, startIn:Directory [, type:String]]]]):Boolean;

Opens a dialogue box with an OS file browser for saving. The file name field is populated with suggestedName. When the user saves a file or cancels, the saved event is triggered and a File object is passed as a property of the information object if successful.

Parameters:

data Data to save.
suggestedName Optional; default name to populate file name field.
startIn Optional; directory handle the file browser will start in. If null the directory will be the last directory used.
encoding Optional; The encoding for the file. Values include "text" (default) or "asobject" (uses .sol format)

Returns

true if the save-as dialogue opened successfully; false otherwise. The likely reason for false being returned is because a file access dialogue is already open.

save

public function save(data:Object, fileHandle:File):Boolean;

aves the data over the file specified by the fileHandle. Depending on the user's player settings, the user may be prompted before the file is overwitten.

Parameters:

data Data to save.
fileHandle The data will be saved over this file.

Returns

true if the file was saved successfully; false otherwise.

open

public function open([suggestedName:String [, startIn:Directory ]]]):Boolean;

Opens a dialogue box with an OS file browser for saving. The file name field is populated with suggestedName. When the user saves a file or cancels, the saved event is triggered and a File object is passed as a property of the information object if successful.

Parameters:

suggestedName Optional; default name to populate file name field.
startIn Optional; directory handle the file browser will start in. If null the directory will be the last directory used.

Returns

true if the open dialogue opened successfully; false otherwise. The likely reason for false being returned is because a file access dialogue is already open

revert

public function revert(fileHandle:File):Boolean;

Immediately updates all properties of a file object, from the version stored on disc.

Parameters:

fileHandle The file to revert

Returns

true if the file was reverted successfully; otherwise false.

getBytesDownloaded

public function getBytesDownloaded():Number;

Immediately updates all properties of a file object, from the version stored on disc.

Parameters:

fileHandle The file to revert

Returns

true if the file was reverted successfully; otherwise false.

getBytesUploaded

public function getBytesUploaded():Number;

Immediately updates all properties of a file object, from the version stored on disc.

Parameters:

fileHandle The file to revert

Returns

true if the file was reverted successfully; otherwise false.

FileSystem.File class

An object representing a file on disc.

Properties

name

public var name:String;

Read-only. Name of the file.

data

public var data:Object;

Read-only. The data stored in the file.

type

public var type:String;

Read-only. Type of file. This will determine the way that a file is encoded when it is saved. Values may include "text" (default text file), "asobject" (uses .sol file format), "binary" (may be read as an array of 16bit numbers).

mediaType

public var mediaType:String;

Read-only. If the file is a supported media type ("FLV", "mp3", "SWF", "JPG" or ,in Central, "gif") then the media type. Otherwise the value is null.

modified

public var modified:Date;

Read-only. The date the file was last modified

created

public var created:Date;

Read-only. The date the file was created

readOnly

public var readOnly:Boolean;

Read-only. true if the file is read-only

deleted

public var deleted:Boolean;

Read-only. true if the file has been deleted since the object was created

directory

public var directory:FileSyatem.Directory;

Read-only. The containing directory of this file. This object may be used to specify this file's parent directory as the directory to start browsing in.

FileSystem.Directory class

This object may be used to specify the directory to start browsing in when opening or "saving-as" a file.

Properties

deleted

public var deleted:Boolean;

Read-only. true if the directory has been deleted since the object was created


Document History :

23/06/04:

06/01/04 - document created