Recipe 23.5.
Filtering Files That Display in the Browser Window
Problem
You want
to filter the type of files
that display in the browse dialog box so the user only sees the
types of files you want to allow her to upload.
Solution
Pass an array of FileFilter objects to the browse( ) method.
Discussion
By default the browse( ) method displays
a dialog box that displays all files on the user's filesystem.
However, you also have the option of filtering the types of files
that are displayed. For example, you may want to display only image
files or only text files. You can accomplish this by passing an
array of flash.net.FileFilter objects to the browse(
) method.
The FileFilter constructor requires at
least two parameters:
File extensions must be semicolon-delimited,
each prepended with an asterisk and a dot. The following constructs
a FileFilter object that filters for files with file
extensions of .png, .gif, and .jpg:
var fileFilter:FileFilter = new FileFilter("Images", "*.png;*.gif;*.jpg");
Additionally, you may opt to specify a third
parameter of Macintosh file types. As with file extensions, file
types are semicolon delimited. When the Macintosh file types
parameter is omitted, the file extensions are used as a filter for
both Windows and Macintosh files. When the Macintosh file types
parameter is specified, the file extensions are used as a filter
for Windows files, while they are ignored for Macintosh files in
preference of the Macintosh file types. Macintosh operating systems
now support file extensions as a way of determining the file type,
yet they also continue to support type codes assigned to the
files.
You can specify which filter (or filters) to use
by passing them as elements of an array to the browse( )
method, as follows:
fileReference.browse([fileFilter]);
In the preceding example, only one filter is
passed to the browse( ) method. Each filter that's passed to
the browse( ) method adds a new option to the "Files of
type" drop-down menu in the browse dialog box. For example, the
following code allows four options in the Files of type
menuImages, Documents, Archives, and
All.
var fileFilter1:FileFilter = new FileFilter("Images", "*.png;*.gif;*.jpg");
var fileFilter2:FileFilter = new FileFilter("Documents", "*.txt;*.doc;*.pdf;*.rtf");
var fileFilter3:FileFilter = new FileFilter("Archives", "*.zip;*.tar;*.hqx");
var fileFilter4:FileFilter = new FileFilter("All", "*.*");
_fileReference.browse([fileFilter1, fileFilter2, fileFilter3, fileFilter4]);
When you pass an array of FileFilter
objects to the browse( ) method, there is a possibility that
the browse( ) method can throw an argument error of type
ArgumentError. Argument errors occur if any of the
FileFilter objects have incorrectly formatted values.
The file filter parameter also works with the
browse( ) method of FileReferenceList.
See Also
Recipe
23.4
|