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

browseForOp<strong>en</strong>Multiple()<br />

These methods are each asynchronous. The browseForOp<strong>en</strong>() and browseForSave() methods dispatch the select<br />

ev<strong>en</strong>t wh<strong>en</strong> the user selects a file (or a target path, in the case of browseForSave()). With the browseForOp<strong>en</strong>() and<br />

browseForSave() methods, upon selection the target File object points to the selected files. The<br />

browseForOp<strong>en</strong>Multiple() method dispatches a selectMultiple ev<strong>en</strong>t wh<strong>en</strong> the user selects files. The<br />

selectMultiple ev<strong>en</strong>t is of type FileListEv<strong>en</strong>t, which has a files property that is an array of File objects (pointing<br />

to the selected files).<br />

For example, the following code pres<strong>en</strong>ts the user with an “Op<strong>en</strong>” dialog box in which the user can select a file:<br />

var fileToOp<strong>en</strong>:File = File.docum<strong>en</strong>tsDirectory;<br />

selectTextFile(fileToOp<strong>en</strong>);<br />

function selectTextFile(root:File):void<br />

{<br />

var txtFilter:FileFilter = new FileFilter("Text", "*.as;*.css;*.html;*.txt;*.xml");<br />

root.browseForOp<strong>en</strong>("Op<strong>en</strong>", [txtFilter]);<br />

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

}<br />

function fileSelected(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

trace(fileToOp<strong>en</strong>.nativePath);<br />

}<br />

If the application has another browser dialog box op<strong>en</strong> wh<strong>en</strong> you call a browse method, the runtime throws an Error<br />

exception.<br />

Note: On Android, only image, video, and audio files can be selected with the browseForOp<strong>en</strong>() and<br />

browseForOp<strong>en</strong>Multiple() methods. The browseForSave() dialog also displays only media files ev<strong>en</strong> though the user<br />

can <strong>en</strong>ter an arbitrary fil<strong>en</strong>ame. For op<strong>en</strong>ing and saving non-media files, you should consider using custom dialogs<br />

instead of these methods.<br />

Modifying File paths<br />

Adobe AIR 1.0 and later<br />

You can also modify the path of an existing File object by calling the resolvePath() method or by modifying the<br />

nativePath or url property of the object, as in the following examples (on Windows):<br />

var file1:File = File.docum<strong>en</strong>tsDirectory;<br />

file1 = file1.resolvePath("AIR Test");<br />

trace(file1.nativePath); // C:\Docum<strong>en</strong>ts and Settings\userName\My Docum<strong>en</strong>ts\AIR Test<br />

var file2:File = File.docum<strong>en</strong>tsDirectory;<br />

file2 = file2.resolvePath("..");<br />

trace(file2.nativePath); // C:\Docum<strong>en</strong>ts and Settings\userName<br />

var file3:File = File.docum<strong>en</strong>tsDirectory;<br />

file3.nativePath += "/subdirectory";<br />

trace(file3.nativePath); // C:\Docum<strong>en</strong>ts and Settings\userName\My Docum<strong>en</strong>ts\subdirectory<br />

var file4:File = new File();<br />

file4.url = "file:///c:/AIR Test/test.txt";<br />

trace(file4.nativePath); // C:\AIR Test\test.txt<br />

Wh<strong>en</strong> using the nativePath property, use the forward slash (/) character as the directory separator character. On<br />

Windows, you can use the backslash (\) character as well, but you should not do so, as it leads to code that does not<br />

work cross-platform.<br />

Last updated 6/6/2012<br />

676

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

Saved successfully!

Ooh no, something went wrong!