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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Working with the file system<br />

Wh<strong>en</strong> all of the data loads from the file, the code calls the onComplete() method. The onComplete() method<br />

removes the list<strong>en</strong>ers for the loading ev<strong>en</strong>ts and th<strong>en</strong> calls the saveFile() method. The saveFile() method calls the<br />

FileRefer<strong>en</strong>ce.save() method. The FileRefer<strong>en</strong>ce.save() method op<strong>en</strong>s a new dialog box to let the user <strong>en</strong>ter<br />

a new fil<strong>en</strong>ame and location to save the file. The remaining ev<strong>en</strong>t list<strong>en</strong>er methods trace the progress of the file saving<br />

process until it is complete.<br />

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

writing files” on page 687.<br />

Uploading files to a server<br />

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

To upload files to a server, first call the browse() method to allow a user to select one or more files. Next, wh<strong>en</strong> the<br />

FileRefer<strong>en</strong>ce.upload() method is called, the selected file is transferred to the server. If the user selects multiple<br />

files using the FileRefer<strong>en</strong>ceList.browse() method, Flash Player creates an array of selected files called<br />

FileRefer<strong>en</strong>ceList.fileList. You can th<strong>en</strong> use the FileRefer<strong>en</strong>ce.upload() method to upload each file<br />

individually.<br />

Note: Using the FileRefer<strong>en</strong>ce.browse() method allows you to upload single files only. To allow a user to upload<br />

multiple files, use the FileRefer<strong>en</strong>ceList.browse() method.<br />

By default, the system file picker dialog box allows users to pick any file type from the local computer. Developers can<br />

specify one or more custom file type filters by using the FileFilter class and passing an array of file filter instances to<br />

the browse() method:<br />

var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;<br />

*.jpeg; *.gif; *.png");<br />

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

var allTypes:Array = new Array(imageTypes, textTypes);<br />

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

fileRef.browse(allTypes);<br />

Wh<strong>en</strong> the user has selected the files and clicked the Op<strong>en</strong> button in the system file picker, the Ev<strong>en</strong>t.SELECT ev<strong>en</strong>t is<br />

dispatched. If the FileRefer<strong>en</strong>ce.browse() method is used to select a file to upload, the following code s<strong>en</strong>ds the<br />

file to a web server:<br />

Last updated 6/6/2012<br />

658

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

Saved successfully!

Ooh no, something went wrong!