Saturday, March 12, 2011

Files and directories in AIR: Part 2

In this tutorial we will learn how to find out if the user selected a file or a directory in the browser window.

To find out when the user makes his selection in the browser dialog window and hits OK, we can use event listeners. For the browseForDirectory, browseForOpen and browseForSave methods, we can use the Event.SELECT listener:

import flash.filesystem.File;

var myFile:File = File.documentsDirectory;
myFile.browseForOpen("Select a file");

myFile.addEventListener(Event.SELECT, fileSelected);

function fileSelected(evt:Event):void{
trace("File: " + evt.target.name + ", " + evt.target.size + " bytes, Full path: " + evt.target.nativePath);
}

If you test your movie now and select a file, you'll get an output that looks something like this:

File: avatar1.gif, 49944 bytes, Full path: C:\Users\7 User\Documents\Kir\avatar1.gif

When you select a file, the File object is changed so that it now refers to the file you have selected, and it also calls the select event, which we listen to. This only works with the browseForDirectory, browseForOpen and browseForSave methods. To listen for the selection in browseForOpenMultiple, you'll need to use a different listener.

When the user selects multiple (or one) files through the OpenMultiple window, a FileListEvent.SELECT_MULTIPLE event is dispatched. When we listen to it, we can return an array of files that the user has selected.

Here's an example:

import flash.filesystem.File;

var myFile:File = File.documentsDirectory;
myFile.browseForOpenMultiple("Select some files");

myFile.addEventListener(FileListEvent.SELECT_MULTIPLE, filesSelected);

function filesSelected(evt:FileListEvent):void
{
trace(evt.files.length + " files have been selected.");
for (var i:int = 0; i < evt.files.length; i++)
{
trace("File: " + evt.files[i].name + ", " + evt.files[i].size + " bytes, Full path: " + evt.files[i].nativePath);
}
}

If you run the code above and select multiple files, you'll receive an output similar to this:

3 files have been selected.
File: avatar1.gif, 49944 bytes, Full path: C:\Users\7 User\Documents\Kir\avatar1.gif
File: avatar1_sq.jpg, 9238 bytes, Full path: C:\Users\7 User\Documents\Kir\avatar1_sq.jpg
File: object.swf, 41121 bytes, Full path: C:\Users\7 User\Documents\Kir\object.swf

And that's it for now.

Thanks for reading!

Related:

Files and directories in AIR: Part 1
Files and directories in AIR: Part 3
Files and directories in AIR: Part 4
Files and directories in AIR: Part 5
Files and directories in AIR: Part 6
Files and directories in AIR: Part 7
Files and directories in AIR: Part 8
Files and directories in AIR: Part 9
Files and directories in AIR: Part 10
Files and directories in AIR: Part 11
Create a log application using XML and AIR: Part 1
Create a log application using XML and AIR: Part 2
Create a log application using XML and AIR: Part 3
Create a log application using XML and AIR: Part 4
Files and directories in AIR: Part 12

3 comments:

Anonymous said...

I look at this blog for as3. I don't want ADOBE AIR?!

Kirill Poletaev said...

I am showing how to use AS3 to create AIR applications. Problem?

yoni said...

just what i was looking for.. thought i had to fileReferenceList for multiple files, which sucks (security and all). thanks

Post a Comment