flash.net

# FileReferenceList - AS3

Package | flash.net
Class | public class FileReferenceList
Inheritance | FileReferenceList EventDispatcher Object

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

The FileReferenceList class provides a means to let users select one or more files for uploading. A FileReferenceList object represents a group of one or more local files on the user's disk as an array of FileReference objects. For detailed information and important considerations about FileReference objects and the FileReference class, which you use with FileReferenceList, see the FileReference class.

To work with the FileReferenceList class:

- Instantiate the class: `var myFileRef = new FileReferenceList();`
- Call the `FileReferenceList.browse()` method, which opens a dialog box that lets the user select one or more files for upload: `myFileRef.browse();`
- After the `browse()` method is called successfully, the `fileList` property of the FileReferenceList object is populated with an array of FileReference objects.
- Call `FileReference.upload()` on each element in the `fileList` array.

The FileReferenceList class includes a `browse()` method and a `fileList` property for working with multiple files. While a call to `FileReferenceList.browse()` is executing, SWF file playback pauses in stand-alone and external versions of Flash Player and in AIR for Linux and Mac OS X 10.1 and earlier.

View the examples

Related API Elements

FileReference

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | fileList : Array [read-only] An array of FileReference objects. | FileReferenceList

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | FileReferenceList() Creates a new FileReferenceList object. | FileReferenceList
|  | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event. | EventDispatcher
|  | browse(typeFilter:Array = null):Boolean Displays a file-browsing dialog box that lets the user select one or more local files to upload. | FileReferenceList
|  | dispatchEvent(event:Event):Boolean Dispatches an event into the event flow. | EventDispatcher
|  | hasEventListener(type:String):Boolean Checks whether the EventDispatcher object has any listeners registered for a specific type of event. | EventDispatcher
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void Removes a listener from the EventDispatcher object. | EventDispatcher
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object
|  | willTrigger(type:String):Boolean Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type. | EventDispatcher

Events
Click for more information on events
Hide Inherited Events
Show Inherited Events

| Event | Summary | Defined By
--- | --- | --- | ---
|  | activate | [broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active. | EventDispatcher
|  | cancel | Dispatched when the user dismisses the file-browsing dialog box. | FileReferenceList
|  | deactivate | [broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive. | EventDispatcher
|  | select | Dispatched when the user selects one or more files to upload from the file-browsing dialog box. | FileReferenceList

Property Detail

### fileList

property

`fileList:Array` [read-only]

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

An array of `FileReference` objects.

When the `FileReferenceList.browse()` method is called and the user has selected one or more files from the dialog box that the `browse()` method opens, this property is populated with an array of FileReference objects, each of which represents the files the user selected. You can then use this array to upload each file with the `FileReference.upload()`method. You must upload one file at a time.

The `fileList` property is populated anew each time browse() is called on that FileReferenceList object.

The properties of `FileReference` objects are described in the FileReference class documentation.

Implementation
`public function get fileList():Array`

Learn more

Using the FileReference class

Related API Elements

FileReference
FileReference.upload()
FileReferenceList.browse()
Constructor Detail

### FileReferenceList

()
Constructor

`public function FileReferenceList()`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Creates a new FileReferenceList object. A FileReferenceList object contains nothing until you call the `browse()` method on it and the user selects one or more files. When you call `browse()` on the FileReference object, the `fileList` property of the object is populated with an array of `FileReference` objects.

Related API Elements

FileReference
FileReferenceList.browse()
Method Detail

### browse

()
method

`public function browse(typeFilter:Array = null):Boolean`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Displays a file-browsing dialog box that lets the user select one or more local files to upload. The dialog box is native to the user's operating system.

In Flash Player 10 and later, you can call this method successfully only in response to a user event (for example, in an event handler for a mouse click or keypress event). Otherwise, calling this method results in Flash Player throwing an Error.

When you call this method and the user successfully selects files, the `fileList` property of this FileReferenceList object is populated with an array of FileReference objects, one for each file that the user selects. Each subsequent time that the FileReferenceList.browse() method is called, the `FileReferenceList.fileList` property is reset to the file(s) that the user selects in the dialog box.

Using the `typeFilter` parameter, you can determine which files the dialog box displays.

Only one `FileReference.browse()`, `FileReference.download()`, or `FileReferenceList.browse()` session can be performed at a time on a FileReferenceList object (because only one dialog box can be opened at a time).

Parameters

| `typeFilter:Array` (default = `null`)`` — An array of FileFilter instances used to filter the files that are displayed in the dialog box. If you omit this parameter, all files are displayed. For more information, see the FileFilter class.

Returns

| `Boolean` — Returns `true` if the parameters are valid and the file-browsing dialog box opens.

Events

| `select:`Event`` — Invoked when the user has successfully selected an item for upload from the dialog box.
| `cancel:`Event`` — Invoked when the user dismisses the dialog box by clicking Cancel or by closing it.

Throws

| `IllegalOperationError` — Thrown for the following reasons: 1) Another FileReference or FileReferenceList browse session is in progress; only one file browsing session may be performed at a time. 2) A setting in the user's mms.cfg file prohibits this operation.
| `ArgumentError` — If the `typeFilter` array does not contain correctly formatted FileFilter objects, an exception is thrown. For details on correct filter formatting, see the FileFilter documentation.
| `Error` — If the method is not called in response to a user action, such as a mouse event or keypress event.

Learn more

Using the FileReference class

Related API Elements

FileReference.browse()
FileReference class
FileFilter class
Event Detail

### cancel

Event

Event Object Type: `flash.events.Event`
property Event.type = `flash.events.Event.CANCEL`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Dispatched when the user dismisses the file-browsing dialog box. (This dialog box opens when you call the `FileReferenceList.browse()`, `FileReference.browse()`, or `FileReference.download()` methods.)

The `Event.CANCEL` constant defines the value of the `type` property of a `cancel` event object.

This event has the following properties:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`target` | A reference to the object on which the operation is canceled.

Related API Elements

FileReferenceList.browse()

### select

Event

Event Object Type: `flash.events.Event`
property Event.type = `flash.events.Event.SELECT`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Dispatched when the user selects one or more files to upload from the file-browsing dialog box. (This dialog box opens when you call the `FileReferenceList.browse()`, `FileReference.browse()`, or `FileReference.download()` methods.) When the user selects a file and confirms the operation (for example, by clicking Save), the `FileReferenceList` object is populated with FileReference objects that represent the files that the user selects.

The `Event.SELECT` constant defines the value of the `type` property of a `select` event object.

This event has the following properties:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`target` | The object on which an item has been selected.

Related API Elements

FileReferenceList.browse()
Examples How to use this example
FileReferenceListExample.as
The following example shows how you can use events to manage the upload of multiple files. The CustomFileReferenceList class extends FileReferenceList and includes a `complete` event, which is dispatched when each individual file in the FileReferenceList object is uploaded. The `LIST_COMPLETE` event in the FileReferenceListExample class is dispatched when all the files in the FileReferenceList object have been uploaded.

To run this example, place a script that is written to accept a file upload at http://www.[yourDomain].com/yourUploadHandlerScript.cfm. Based on the location of your SWF file and where you are uploading files to, you also might need to compile the SWF file with Local Playback Security set to Access Network Only or update Flash® Player security settings to allow this file network access. If your upload server is remote and you run this example from your desktop computer, your server must have a crossdomain.xml file.

Copy

    package {
        import flash.display.Sprite;
        import flash.events.*;
        import flash.net.FileReference;
        import flash.net.FileReferenceList;

        public class FileReferenceListExample extends Sprite {
            public static var LIST_COMPLETE:String = "listComplete";
            public function FileReferenceListExample() {
                initiateFileUpload();
            }

            private function initiateFileUpload():void {
                var fileRef:CustomFileReferenceList = new CustomFileReferenceList();
                fileRef.addEventListener(FileReferenceListExample.LIST_COMPLETE, listCompleteHandler);
                fileRef.browse(fileRef.getTypes());
            }

            private function listCompleteHandler(event:Event):void {
                trace("listCompleteHandler");
            }
        }
    }

    import flash.events.*;
    import flash.net.FileReference;
    import flash.net.FileReferenceList;
    import flash.net.FileFilter;
    import flash.net.URLRequest;

    class CustomFileReferenceList extends FileReferenceList {
        private var uploadURL:URLRequest;
        private var pendingFiles:Array;

        public function CustomFileReferenceList() {
            uploadURL = new URLRequest();
            uploadURL.url = "http://www.[yourDomain].com/yourUploadHandlerScript.cfm";
            initializeListListeners();
        }

        private function initializeListListeners():void {
            addEventListener(Event.SELECT, selectHandler);
            addEventListener(Event.CANCEL, cancelHandler);
        }

        public function getTypes():Array {
            var allTypes:Array = new Array();
            allTypes.push(getImageTypeFilter());
            allTypes.push(getTextTypeFilter());
            return allTypes;
        }

        private function getImageTypeFilter():FileFilter {
            return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
        }

        private function getTextTypeFilter():FileFilter {
            return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
        }

        private function doOnComplete():void {
            var event:Event = new Event(FileReferenceListExample.LIST_COMPLETE);
            dispatchEvent(event);
        }

        private function addPendingFile(file:FileReference):void {
            trace("addPendingFile: name=" + file.name);
            pendingFiles.push(file);
            file.addEventListener(Event.OPEN, openHandler);
            file.addEventListener(Event.COMPLETE, completeHandler);
            file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            file.upload(uploadURL);
        }

        private function removePendingFile(file:FileReference):void {
            for (var i:uint; i < pendingFiles.length; i++) {
                if (pendingFiles[i].name == file.name) {
                    pendingFiles.splice(i, 1);
                    if (pendingFiles.length == 0) {
                        doOnComplete();
                    }
                    return;
                }
            }
        }

        private function selectHandler(event:Event):void {
            trace("selectHandler: " + fileList.length + " files");
            pendingFiles = new Array();
            var file:FileReference;
            for (var i:uint = 0; i < fileList.length; i++) {
                file = FileReference(fileList[i]);
                addPendingFile(file);
            }
        }

        private function cancelHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("cancelHandler: name=" + file.name);
        }

        private function openHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("openHandler: name=" + file.name);
        }

        private function progressHandler(event:ProgressEvent):void {
            var file:FileReference = FileReference(event.target);
            trace("progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function completeHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("completeHandler: name=" + file.name);
            removePendingFile(file);
        }

        private function httpErrorHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("httpErrorHandler: name=" + file.name);
        }

        private function ioErrorHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("ioErrorHandler: name=" + file.name);
        }

        private function securityErrorHandler(event:Event):void {
            var file:FileReference = FileReference(event.target);
            trace("securityErrorHandler: name=" + file.name + " event=" + event.toString());
        }
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00