Recipe 23.6. Detecting When the User Has Selected a File to Upload


Problem

You want to detect when the user has selected a file from the browse dialog box.

Solution

Listen for a select event. Listen for a cancel event to determine if and when the user clicks the Cancel button.

Discussion

When the user selects a file and clicks the Open button from a browse dialog box, the FileReference object dispatches a select event of type Event. You can use the Event.SELECT constant to add a listener, as follows:

fileReference.addEventListener(Event.SELECT, onSelectFile);

When the user has selected a file, the details of that file are immediately available (name, size, createdDate, etc.). For example, as soon as a FileReference object dispatches a SELECT event you can retrieve the filename, as in the following example:

selectedFileTextField.text = fileReference.name;

When the user clicks the Cancel button from a browse dialog box, the FileReference object dispatches a cancel event of type Event. You can use the Event.CANCEL constant to add a listener, as follows:

fileReference.addEventListener(Event.CANCEL, onCancelBrowse);

The select and cancel events also work for FileReferenceList objects.

See Also

Recipe 23.7




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