13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with the file system<br />

}<br />

}<br />

{<br />

}<br />

trace("File was successfully loaded.");<br />

trace(fileRef.data);<br />

public function onCancel(evt:Ev<strong>en</strong>t):void<br />

{<br />

trace("The browse request was canceled by the user.");<br />

}<br />

public function onIOError(evt:IOErrorEv<strong>en</strong>t):void<br />

{<br />

trace("There was an IO Error.");<br />

}<br />

public function onSecurityError(evt:Ev<strong>en</strong>t):void<br />

{<br />

trace("There was a security error.");<br />

}<br />

The example code first creates the FileRefer<strong>en</strong>ce object named fileRef and th<strong>en</strong> calls its browse() method. The<br />

browse() method op<strong>en</strong>s a dialog box that prompts the user to select a file. Wh<strong>en</strong> a file is selected, the code invokes the<br />

onFileSelected() method. This method adds list<strong>en</strong>ers for the progress and complete ev<strong>en</strong>ts and th<strong>en</strong> calls the<br />

FileRefer<strong>en</strong>ce object’s load() method. The other handler methods in the example simply output messages to report<br />

on the progress of the load operation. Wh<strong>en</strong> the loading completes, the application displays the cont<strong>en</strong>ts of the loaded<br />

file using the trace() method.<br />

In Adobe AIR, the FileStream class provides additional functionality for reading data from a local file. See “Reading<br />

and writing files” on page 687.<br />

Saving data to local files<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The FileRefer<strong>en</strong>ce.save() method lets you save data to a local file. It starts by op<strong>en</strong>ing a dialog box to let the user<br />

<strong>en</strong>ter a new fil<strong>en</strong>ame and location to which to save a file. After the user selects the fil<strong>en</strong>ame and location, the data is<br />

writt<strong>en</strong> to the new file. Wh<strong>en</strong> the file is saved successfully, the properties of the FileRefer<strong>en</strong>ce object are populated with<br />

the properties of the local file.<br />

Note: Your code can only call the FileRefer<strong>en</strong>ce.save() method in response to a user-initiated ev<strong>en</strong>t such as a mouse<br />

click or a keypress ev<strong>en</strong>t. Otherwise an error is thrown. This restriction does not apply to cont<strong>en</strong>t running in Adobe AIR<br />

in the application security sandbox.<br />

The FileRefer<strong>en</strong>ce.save() method returns immediately after being called. The FileRefer<strong>en</strong>ce object th<strong>en</strong><br />

dispatches ev<strong>en</strong>ts to call list<strong>en</strong>er methods at each step of the file saving process.<br />

The FileRefer<strong>en</strong>ce object dispatches the following ev<strong>en</strong>ts during the file saving process:<br />

select ev<strong>en</strong>t (Ev<strong>en</strong>t.SELECT): Dispatched wh<strong>en</strong> the user specifies the location and file name for the new file to be<br />

saved.<br />

cancel ev<strong>en</strong>t (Ev<strong>en</strong>t.CANCEL): Dispatched wh<strong>en</strong> the user click the Cancel button in the dialog box.<br />

op<strong>en</strong> ev<strong>en</strong>t (Ev<strong>en</strong>t.OPEN): Dispatched wh<strong>en</strong> the save operation starts.<br />

progress ev<strong>en</strong>t (ProgressEv<strong>en</strong>t.PROGRESS): Dispatched periodically as bytes of data are saved to the file.<br />

Last updated 6/6/2012<br />

655

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!