Friday, March 11, 2011

Files and directories in AIR: Part 1

Today we'll learn about letting the user select files and directories using a browse dialog window.

There are 4 methods to call out the browse dialog window, and those are:

  • browseForDirectory()
  • browseForSave()
  • browseForOpen()
  • browseForOpenMultiple()

To call any of the methods above, we need to define a File object first. For example, let's create a File object and refer it to the user's documents directory - when we call out the browseForDirectory, it has the documents directory selected by default. You can pass the text to be displayed in the dialog window as a parameter in the method.

import flash.filesystem.File;

var myFile:File = File.documentsDirectory;
myFile.browseForDirectory("Select a directory");



The browseForOpen and browseForOpenMultiple are very similar methods, except for the fact that the first one only allows one file to be selected, and the second - multiple files. They have two parameters that can be passed - one is the text description of the browse window, the second one for the filter array. The filter array is used to only allow files with certain extensions to be selected by the user, for example, only JPG files. We can make multiple filters.

import flash.filesystem.File;

var myFile:File = File.documentsDirectory;
myFile.browseForOpen("Select one file", [
 new FileFilter("JPG Images", "*.jpg;*.jpeg"),
 new FileFilter("GIF Images", "*.gif"),
 new FileFilter("PNG Images", "*.png"),
 new FileFilter("All Images", "*.jpg;*.jpeg;*.gif;*.png")]);



The browseForSave method only has one parameter, which is the title of the browser window.

import flash.filesystem.File;

var myFile:File = File.documentsDirectory;
myFile.browseForSave("Save the file");



Thanks for reading!

Related:

Files and directories in AIR: Part 2
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

No comments:

Post a Comment