Recipe 23.4. Browsing for Files


Problem

You want to let the user browse his local disk(s) for a file (s) to upload.

Solution

Use the browse( ) method of either a FileReference or FileReferenceList object.

Discussion

Using the browse( ) method of a FileReference of FileReferenceList object, you can open a dialog box that allows the user to browse his local disks for a file or files. The difference between the browse( ) methods of the FileReference and FileReferenceList classes is that the former allows the user to select just one file, while the latter allows the user to select more than one file by holding the Control or Shift key (or the Command key on a Macintosh):

fileReference.browse(  );

As with the download( ) method, the browse( ) method can throw errors, and it's best to handle the errors using try/catch. An illegal operation error can occur in one of two scenarios:

  • Only one browse dialog box can be open at a time. If a dialog box is already open and you call browse( ), it throws an error of type IllegalOperationError.

  • If the user has configured his global Flash Player settings to disallow file browsing, calling the browse( ) method will throw an error.

This block of code illustrates how to use a TRy/catch block to handle an IllegalOperationError thrown when calling browse( ).

try {     fileReference.browse(  ); } catch (illegalOperation:IllegalOperationError) {     // code to handle error }

Handling the error ensures that Flash Player will not display a default error message to the user (which could be disconcerting to the user).

See Also

Recipe 23.5




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net