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 />

The FileRefer<strong>en</strong>ce.load() method returns immediately after being called, but the data being loaded isn’t available<br />

immediately. The FileRefer<strong>en</strong>ce object dispatches ev<strong>en</strong>ts to invoke list<strong>en</strong>er methods at each step of the loading process.<br />

The FileRefer<strong>en</strong>ce object dispatches the following ev<strong>en</strong>ts during the loading process.<br />

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

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

complete ev<strong>en</strong>t (Ev<strong>en</strong>t.COMPLETE): Dispatched wh<strong>en</strong> the load operation completes successfully.<br />

ioError ev<strong>en</strong>t (IOErrorEv<strong>en</strong>t.IO_ERROR): Dispatched if the load process fails because an input/output error<br />

occurs while op<strong>en</strong>ing or reading data from the file.<br />

Once the FileRefer<strong>en</strong>ce object dispatches the complete ev<strong>en</strong>t, the loaded data can be accessed as a ByteArray in the<br />

FileRefer<strong>en</strong>ce object’s data property.<br />

The following example shows how to prompt the user to select a file and th<strong>en</strong> load the data from that file into memory:<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.ev<strong>en</strong>ts.*;<br />

import flash.net.FileFilter;<br />

import flash.net.FileRefer<strong>en</strong>ce;<br />

import flash.net.URLRequest;<br />

import flash.utils.ByteArray;<br />

public class FileRefer<strong>en</strong>ceExample1 ext<strong>en</strong>ds Sprite<br />

{<br />

private var fileRef:FileRefer<strong>en</strong>ce;<br />

public function FileRefer<strong>en</strong>ceExample1()<br />

{<br />

fileRef = new FileRefer<strong>en</strong>ce();<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.SELECT, onFileSelected);<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.CANCEL, onCancel);<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(IOErrorEv<strong>en</strong>t.IO_ERROR, onIOError);<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(SecurityErrorEv<strong>en</strong>t.SECURITY_ERROR,<br />

onSecurityError);<br />

var textTypeFilter:FileFilter = new FileFilter("Text Files (*.txt, *.rtf)",<br />

"*.txt;*.rtf");<br />

fileRef.browse([textTypeFilter]);<br />

}<br />

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

{<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(ProgressEv<strong>en</strong>t.PROGRESS, onProgress);<br />

fileRef.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, onComplete);<br />

fileRef.load();<br />

}<br />

public function onProgress(evt:ProgressEv<strong>en</strong>t):void<br />

{<br />

trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");<br />

}<br />

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

Last updated 6/6/2012<br />

654

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

Saved successfully!

Ooh no, something went wrong!