FileReference (flash.net.FileReference)


Object
    |
    +-flash.net.FileReference

public class FileReference
extends Object

The FileReference class provides a means to upload and download files between a user's computer and a server. An operating-system dialog box prompts the user to select a file to upload or a location for download. Each FileReference object refers to a single file on the user's hard disk and has properties that contain information about the file's size, type, name, creation date, modification date, and creator type (Macintosh only).

FileReference instances are created in two ways:

During an upload operation, all of the properties of a FileReference object are populated by calls to FileReference.browse() or FileReferenceList.browse(). During a download operation, the name property is populated when onSelect has been invoked; all other properties are populated when onComplete has been invoked.

The browse() method opens an operating-system dialog box which prompts the user to select any local file for upload. The FileReference.browse() method lets the user select a single file; the FileReferenceList.browse() method lets the user select multiple files. After a successful call to the browse() method, call the FileReference.upload() method to upload one file at a time. The FileReference.download() method prompts the user for a location to save the file and initiates downloading from a remote URL.

The FileReference and FileReferenceList classes do not let you set the default file location for the dialog box generated by browse() and download() calls. The default location shown in the dialog box is the most recently browsed folder, if that location can be determined, or the desktop. The classes do not allow you to read from or write to the transferred file. They do not allow the SWF file that initiated the upload or download to access the uploaded or downloaded file or the file's location on the user's disk.

The FileReference and FileReferenceList classes also do not provide methods for authentication. With servers that require authentication, you can download files with the Flash Player browser plug-in, but uploading (on all players) and downloading (on the stand-alone or external player) fails. Use FileReference event listeners to ascertain whether operations have successfully completed and to handle errors.

For uploading and downloading operations, a SWF file can access files only within its own domain, including any domains that are specified by a cross-domain policy file. If the SWF that is initiating the upload or download doesn't come from the same domain as the file server, you must put a policy file on the file server.

While calls to the FileReference.browse(), FileReferenceList.browse(), or FileReference.download()methods are executing, SWF file playback pauses on the following platforms: Flash Players for Linux, the Flash Player plug-in for Mac OS X, the external Flash Player for Macintosh, and the stand-alone player for Mac OS X 10.1 and earlier. The SWF file continues to run in all players for Windows and in the stand-alone player for Macintosh on Mac OS X 10.2 and later.

Availability: ActionScript 1.0; Flash Player 8

Example

The following example creates a FileReference object that prompts the user to select an image or text file to be uploaded. It also listens for any possible event.

import flash.net.FileReference;

var allTypes:Array = new Array();
var imageTypes:Object = new Object();
imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
allTypes.push(imageTypes);

var textTypes:Object = new Object();
textTypes.description = "Text Files (*.txt, *.rtf)";
textTypes.extension = "*.txt;*.rtf";
allTypes.push(textTypes);

var listener:Object = new Object(); 

listener.onSelect = function(file:FileReference):Void {
    trace("onSelect: " + file.name);
    if(!file.upload("http://www.yourdomain.com/yourUploadHandlerScript.cfm")) {
        trace("Upload dialog failed to open.");
    }
}

listener.onCancel = function(file:FileReference):Void {
    trace("onCancel");
}

listener.onOpen = function(file:FileReference):Void {
    trace("onOpen: " + file.name);
}

listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    trace("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal);
}

listener.onComplete = function(file:FileReference):Void {
    trace("onComplete: " + file.name);
}

listener.onHTTPError = function(file:FileReference):Void {
    trace("onHTTPError: " + file.name);
}

listener.onIOError = function(file:FileReference):Void {
    trace("onIOError: " + file.name);
}

listener.onSecurityError = function(file:FileReference, errorString:String):Void {
    trace("onSecurityError: " + file.name + " errorString: " + errorString);
}

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
fileRef.browse(allTypes);

See also

FileReferenceList (flash.net.FileReferenceList)

Property summary

Modifiers

Property

Description

 

creationDate:Date [read-only]

The creation date of the file on the local disk.

 

creator:String [read-only]

The Macintosh creator type of the file.

 

modificationDate:Date [read-only]

The date that the file on the local disk was last modified.

 

name:String [read-only]

The name of the file on the local disk.

 

postData:String

POST parameters to submit with the upload or download.

 

size:Number [read-only]

The size of the file on the local disk, in bytes.

 

type:String [read-only]

The file type.

Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


Event summary

Event

Description

onCancel = function(fileRef:FileReference) {}

Invoked when the user dismisses the file-browsing dialog box.

onComplete = function(fileRef:FileReference) {}

Invoked when the upload or download operation has successfully completed.

onHTTPError = function(fileRef:FileReference, httpError:Number) {}

Invoked when an upload fails because of an HTTP error.

onIOError = function(fileRef:FileReference) {}

Invoked when an input/output error occurs.

onOpen = function(fileRef:FileReference) {}

Invoked when an upload or download operation starts.

onProgress = function(fileRef:FileReference, bytesLoaded:Number, bytesTotal:Number) {}

Invoked periodically during the file upload or download operation.

onSecurityError = function(fileRef:FileReference, errorString:String) {}

Invoked when an upload or download fails because of a security error.

onSelect = function(fileRef:FileReference) {}

Invoked when the user selects a file to upload or download from the file-browsing dialog box.

onUploadCompleteData = function(fileRef:FileReference, data:String) {}

Invoked after data is received from the server after a successful upload.

Constructor summary

Signature

Description

FileReference()

Creates a new FileReference object.

Method summary

Modifiers

Signature

Description

 

addListener(listener:Object) : Void

Registers an object to receive notification when a FileReference event listener is invoked.

 

browse([typelist:Array]) : Boolean

Displays a file-browsing dialog box in which the user can select a local file to upload.

 

cancel() : Void

Cancels any ongoing upload or download operation on this FileReference object.

 

download(url:String, [defaultFileName:String]) : Boolean

Displays a dialog box in which the user can download a file from a remote server.

 

removeListener(listener:Object) : Boolean

Removes an object from the list of objects that receive event notification messages.

 

upload(url:String, uploadDataFieldName:String, testUpload:Boolean) : Boolean

Starts the upload of a file selected by a user to a remote server.

Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)



Flash CS3