Packageflash.net
Classpublic class FileReference
InheritanceFileReference Inheritance Object

Player version: Flash Player 8

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 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.


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


Public Properties
 Property
  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
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
Creates a new FileReference object.
  
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.
  
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, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Events
 EventSummaryDefined by
  
onCancel = function(fileRef:FileReference) {}
Invoked when the user dismisses the file-browsing dialog box.FileReference
  
onComplete = function(fileRef:FileReference) {}
Invoked when the upload or download operation has successfully completed.FileReference
  
onHTTPError = function(fileRef:FileReference, httpError:Number) {}
Invoked when an upload fails because of an HTTP error.FileReference
  
onIOError = function(fileRef:FileReference) {}
Invoked when an input/output error occurs.FileReference
  
onOpen = function(fileRef:FileReference) {}
Invoked when an upload or download operation starts.FileReference
  
onProgress = function(fileRef:FileReference, bytesLoaded:Number, bytesTotal:Number) {}
Invoked periodically during the file upload or download operation.FileReference
  
onSecurityError = function(fileRef:FileReference, errorString:String) {}
Invoked when an upload or download fails because of a security error.FileReference
  
onSelect = function(fileRef:FileReference) {}
Invoked when the user selects a file to upload or download from the file-browsing dialog box.FileReference
  
onUploadCompleteData = function(fileRef:FileReference, data:String) {}
Invoked after data is received from the server after a successful upload.FileReference
Property detail
creationDateproperty
creationDate:Date  [read-only]

Player version: Flash Player 8

The creation date of the file on the local disk. If the FileReference object has not been populated, a call to get the value of this property returns null.

Implementation
    public function get creationDate():Date

See also


Example
The following example retrieves the creation date of a file selected by the user.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
    trace("creationDate: " + file.creationDate);
}

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

creatorproperty 
creator:String  [read-only]

Player version: Flash Player 8

The Macintosh creator type of the file. In Windows, this property is null. If the FileReference object has not been populated, a call to get the value of this property returns null.

Implementation
    public function get creator():String

See also


Example
The following example retrieves the Macintosh creator type of a file selected by the user.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
    trace("creator: " + file.creator);
}

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

modificationDateproperty 
modificationDate:Date  [read-only]

Player version: Flash Player 8

The date that the file on the local disk was last modified. If the FileReference object has not been populated, a call to get the value of this property returns null.

Implementation
    public function get modificationDate():Date

See also


Example
The following example retrieves the modificationDate of a file selected by the user.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
    trace("modificationDate: " + file.modificationDate);
}

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

nameproperty 
name:String  [read-only]

Player version: Flash Player 8

The name of the file on the local disk. If the FileReference object has not been populated, a call to get the value of this property returns null.

All the properties of a FileReference object are populated by calling browse(). Unlike other FileReference properties, if you call download(), the name property is populated when onSelect is invoked.

Implementation
    public function get name():String

See also


Example
The following example retrieves the name of a file selected by the user.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
    trace("name: " + file.name);
}

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

postDataproperty 
public var postData:String

Player version: Flash Player 8

POST parameters to submit with the upload or download.

See also

sizeproperty 
size:Number  [read-only]

Player version: Flash Player 8

The size of the file on the local disk, in bytes. If the FileReference object has not been populated, a call to get the value of this property returns null.

Implementation
    public function get size():Number

See also


Example
The following example retrieves the size of a file selected by the user.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
    trace("size: " + file.size + " bytes");
}

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

typeproperty 
type:String  [read-only]

Player version: Flash Player 8

The file type. In Windows, this property is the file extension. On the Macintosh, this property is the four-character file type. If the FileReference object has not been populated, a call to get the value of this property returns null.

Implementation
    public function get type():String

See also


Example
The following example retrieves the type of a file selected by the user.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void {
    trace("type: " + file.type);
}

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

Constructor detail
FileReference()constructor
public function FileReference()

Player version: Flash Player 8

Creates a new FileReference object. When populated, a FileReference object represents a file on the user's local disk.

See also


Example
The following example creates a new FileReference object and initiates the download of a PDF file.
import flash.net.FileReference;

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

var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
fileRef.download(url, "FlashPlatform.pdf");

Method detail
addListener()method
public function addListener(listener:Object):Void

Player version: Flash Player 8

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

Parameters
listener:Object — An object that listens for a callback notification from the FileReference event listeners.

Example
The following example adds a listener to an instance of FileReference.
import flash.net.FileReference;

var listener:Object = new Object();

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);
}

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
fileRef.download(url, "FlashPlatform.pdf");

browse()method 
public function browse([typelist:Array]):Boolean

Player version: Flash Player 8

Displays a file-browsing dialog box in which the user can select a local file to upload. The dialog box is native to the user's operating system. When you call this method and the user successfully selects a file, the properties of this FileReference object are populated with the properties of that file. Each subsequent time that FileReference.browse() is called, the FileReference object's properties are reset to the file selected by the user in the dialog box.

You can pass an array of file types to determine which files the dialog box displays.

Only one browse() session can be performed at a time (because only one dialog box can be displayed at a time).

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.

Because of new functionality added to Flash Player, when publishing to Flash Player 10, you can have only one of the following operations active at one time: FileReference.browse(), FileReference.upload(), FileReference.download(). Otherwise, Flash Player throws a runtime error (code 2174). Use FileReference.cancel() to stop an operation in progress. This restriction applies only to Flash Player 10. Previous versions of Flash Player are unaffected by this restriction on simultaneous multiple operations.

Parameters
typelist:Array [optional] — An array of file types used to filter the files displayed in the dialog box. If you omit this parameter, all files are displayed. If you include this parameter, the array must contain one or more elements enclosed in curly braces { }. You can use one of two formats for the array:
  • A list of file type descriptions followed by their Windows file extensions only.
    Each element in the array must contain a string that describes the file type and a semicolon-delimited list of Windows file extensions, with a wildcard character (*) preceding each extension. The syntax for each element is as follows:
    [{description: "string describing the first set of file types", extension: "semicolon-delimited list of file extensions"}]
    Example:
    [{description: "Images", extension: "*.jpg;*.gif;*.png"}, {description: "SWF files", extension: "*.swf"}, {description: "Documents", extension: "*.doc;*.pdf"}]
  • A list of file type descriptions followed by their Windows file extensions and their Macintosh file types.
    Each element in the array must contain a string that describes the file type; a semicolon-delimited list of Windows file extensions, with a wildcard character (*) preceding each extension; and a semicolon-delimited list of Macintosh file types, with a wildcard character (*) preceding each type. The syntax for each element is as follows:
    [{description: "string describing the first set of file types", extension: "semicolon-delimited list of Windows file extensions", macType: "semicolon-delimited list of Macintosh file types"}]
    Example:
    [{description: "Image files", extension: "*.jpg;*.gif;*.png", macType: "JPEG;jp2_;GIFF"}, {description: "SWF files", extension: "*.swf", macType: "SWFL"}]

The two formats are not interchangeable in a single browse() call. You must use one or the other.

The list of extensions is used to filter the files in Windows, depending on the file selected by the user. It is not actually displayed in the dialog box. To display the file types for users, you must list the file types in the description string as well as in the extension list. The description string is displayed in the dialog box in Windows. (It is not used on the Macintosh.) On the Macintosh, if you supply a list of Macintosh file types, that list is used to filter the files. If you don't supply a list of Macintosh file types, the list of Windows extensions is used.

Returns
Boolean — Returns true if the parameters are valid and the file-browsing dialog box is displayed. Returns false if the dialog box is not displayed, if another browse session is already in progress, or if you use the typelist parameter but fail to provide a description or extension string in any element in the array.

See also


Example
The following example displays a dialog box in which the user can select an image file to be uploaded.
import flash.net.FileReference;

var listener:Object = new Object();
listener.onSelect = function(file:FileReference):Void  {
    trace("Opened " + file.name);
}

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

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

cancel()method 
public function cancel():Void

Player version: Flash Player 8

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


Example
The following example downloads approximately half of the requested file and then cancels the download. This is obviously not a typical usage. You might more often use this method to allow users to click Cancel in a download status dialog box.
import flash.net.FileReference;

var listener:Object = new Object();

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

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
fileRef.download(url, "FlashPlatform.pdf");

download()method 
public function download(url:String, [defaultFileName:String]):Boolean

Player version: Flash Player 8

Displays a dialog box in which the user can download a file from a remote server. Although Flash Player has no restriction on the size of files you can upload or download, the player officially supports uploads or downloads of up to 100 MB.

You cannot connect to commonly reserved ports. For a complete list of blocked ports, see the system.Security.loadPolicyFile() entry.

This method first opens an operating system dialog box that asks the user to enter a filename and select a location on the local computer to save the file. When the user selects a location and confirms the download operation (for example, by clicking Save), the download from the remote server begins. Listeners receive events to indicate the progress, success, or failure of the download. To ascertain the status of the dialog box and the download operation after calling download(), your ActionScript code must listen for events by using event listeners such as onCancel, onOpen, onProgress, and onComplete.

The FileReference.upload() and FileReference.download() functions are nonblocking. These functions return after they are called, before the file transmission is complete. In addition, if the FileReference object goes out of scope, any upload or download that has not yet been completed on that object is cancelled upon leaving the scope. So, be sure that your FileReference object will remain in scope for as long as the upload or download could be expected to continue.

When the file has successfully downloaded, the properties of the FileReference object are populated with the properties of the local file and the onComplete listener is invoked.

Only one download() session can be performed at a time (because only one dialog box can be displayed at a time).

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.

Because of new functionality added to Flash Player, when publishing to Flash Player 10, you can have only one of the following operations active at one time: FileReference.browse(), FileReference.upload(), FileReference.download(). Otherwise, Flash Player throws a runtime error (code 2174). Use FileReference.cancel() to stop an operation in progress. This restriction applies only to Flash Player 10. Previous versions of Flash Player are unaffected by this restriction on simultaneous multiple operations.

This method supports downloading of any file type, with either HTTP or HTTPS.

To send POST parameters to the server, set the value of FileReference.postData to your parameters. You can also send GET parameters to the server with the download() call by appending parameters to the URL, for the server script to parse.

Note: If your server requires user authentication, only SWF files that are running in a browser—that is, using the browser plug-in or ActiveX control—can provide a dialog box to prompt the user for a user name and password for authentication, and only for downloads. For uploads using the plug-in or ActiveX control, and for uploads and downloads using the stand-alone or external player, the file transfer fails.

When using this method, consider the Flash Player security model:

For more information related to security, see the following:

Parameters
url:String — The URL of the file to download to the local computer. You can send GET parameters to the server with the download() call by appending parameters to the URL, for the server script to parse. For example:
http://www.myserver.com/picture.jpg?userID=jdoe

On some browsers, URL strings are limited in length. Lengths greater than 256 characters may fail on some browsers or servers.

 
defaultFileName:String [optional] — The default filename displayed in the dialog box, for the file to be downloaded. This string cannot contain the following characters: / \ : * ? " < > | %

If you omit this parameter, the filename of the remote URL is parsed out and used as the default.

Returns
Boolean — A value of true if the dialog box in which a user can select a file is displayed. If the dialog box is not displayed, the method returns false. The dialog box could fail to be displayed for any of the following reasons:
  • You did not pass a value for the url parameter.
  • The parameters passed are of the incorrect type or format.
  • The url parameter has a length of 0.
  • A security violation occurred; your SWF file attempted to access a file from a server that is outside your SWF file's security sandbox.
  • Another browse session is already in progress. A browse session can be started by FileReference.browse(), FileReferenceList.browse(), or FileReference.download().
  • The protocol is not HTTP or HTTPS.

See also


Example
The following example attempts to download a file using the download method. Notice that there are listeners for all of the events.
import flash.net.FileReference;

var listener:Object = new Object();

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

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.onIOError = function(file:FileReference):Void {
    trace("onIOError: " + file.name);
}

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
if(!fileRef.download(url, "FlashPlatform.pdf")) {
    trace("dialog box failed to open.");
}

removeListener()method 
public function removeListener(listener:Object):Boolean

Player version: Flash Player 8

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

Parameters
listener:Object — An object that listens for a callback notification from the FileReference event listeners.

Returns
Boolean — Returns true if the object specified in the listener parameter was successfully removed. Otherwise, this method returns false.

Example
The following example removes an event listener using the removeListener method. If a user cancels the download, the listener is removed so that it no longer receives events from that FileReference object.
import flash.net.FileReference;

var listener:Object = new Object();

listener.onCancel = function(file:FileReference):Void {
    trace(file.removeListener(this)); // true
}

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
fileRef.download(url, "FlashPlatform.pdf");

upload()method 
public function upload(url:String, uploadDataFieldName:String, testUpload:Boolean):Boolean

Player version: Flash Player 8

Starts the upload of a file selected by a user to a remote server. Although Flash Player has no restriction on the size of files you can upload or download, the player officially supports uploads or downloads of up to 100 MB. You must call FileReference.browse() or FileReferenceList.browse() before calling this method.

You cannot connect to commonly reserved ports. For a complete list of blocked ports, see the system.Security.loadPolicyFile() entry.

Listeners receive events to indicate the progress, success, or failure of the upload. Although you can use the FileReferenceList object to let users select multiple files to upload, you must upload the files one by one. To do so, iterate through the FileReferenceList.fileList array of FileReference objects.

The FileReference.upload() and FileReference.download() functions are nonblocking. These functions return after they are called, before the file transmission is complete. In addition, if the FileReference object goes out of scope, any upload or download that has not yet been completed on that object is cancelled upon leaving the scope. So, be sure that your FileReference object will remain in scope for as long as the upload or download could be expected to continue.

Because of new functionality added to Flash Player, when publishing to Flash Player 10, you can have only one of the following operations active at one time: FileReference.browse(), FileReference.upload(), FileReference.download(). Otherwise, Flash Player throws a runtime error (code 2174). Use FileReference.cancel() to stop an operation in progress. This restriction applies only to Flash Player 10. Previous versions of Flash Player are unaffected by this restriction on simultaneous multiple operations.

The file is uploaded to the URL passed in the url parameter. The URL must be a server script configured to accept uploads. Flash Player uploads files using the HTTP POST method. The server script that handles the upload should expect a POST request with the following elements:

Here is a sample POST request:

     Content-Type: multipart/form-data; boundary=AaB03x
     --AaB03x 
     Content-Disposition: form-data; name="Filedata"; filename="example.jpg" 
     Content-Type: application/octet-stream
     ... contents of example.jpg ... 
     --AaB03x-- 
     

To send POST parameters to the server, set the value of FileReference.postData to your parameters. You can send GET parameters to the server with the upload() call by appending parameters to the URL.

If the file to be uploaded is bigger than approximately 10 KB, Windows Flash Player versions first send a test upload POST with zero content prior to uploading the actual file in order to verify that the transmission is likely to be successful. The second POST contains an actual file content. For smaller files, Flash Player does a single upload POST with the file to be uploaded. The Macintosh players currently do not do test upload POSTs.

Note: If your server requires user authentication, only SWF files running in a browser—that is, using the browser plug-in or ActiveX control—can provide a dialog box to prompt the user for a user name and password for authentication, and only for downloads. For uploads that use the plug-in or ActiveX control, and for uploads and downloads that use the stand-alone or external player, the file transfer fails.

When using this method, consider the Flash Player security model:

For more information related to security, see the following:

Parameters
url:String — The URL of the server script configured to handle upload through HTTP POST calls. The URL can be HTTP or, for secure uploads, HTTPS.
 
uploadDataFieldName:String — The field name that precedes the file data in the upload POST. This parameter is supported in Flash Player 9.0.28.0 and later, only. The uploadDataFieldName value must be non-null and a non-empty String. By default, the value of uploadDataFieldName is "Filedata":
     Content-Type: multipart/form-data; boundary=AaB03x
     --AaB03x 
     Content-Disposition: form-data; name="Filedata"; filename="example.jpg" 
     Content-Type: application/octet-stream
     ... contents of example.jpg ... 
     --AaB03x-- 
     
 
testUpload:Boolean — A setting to request a test file upload. If testUpload is true, then for files larger than 10 KB, Flash Player will attempt a test file upload POST with a Content-Length of 0. The purpose of the test upload is to check whether the actual file upload will be successful and whether server authentication, if required, will succeed. By default, testUpload is false. At this time, a test upload is done only for the Windows players.

You can send GET parameters to the server with the upload() call by appending parameters to the URL; for example, http://www.myserver.com/upload.cgi?userID=jdoe

On some browsers, URL strings are limited in length. Lengths greater than 256 characters may fail on some browsers or servers.

Returns
Boolean — A value of false in any of the following situations:
  • FileReference.browse() has not yet been successfully called on this object, or FileReferenceList.browse() has not yet been successfully called with this object in its filelist array.
  • The protocol is not HTTP or HTTPS.
  • A security violation occurs; your SWF file attempts to access a file from a server that is outside your SWF file's security sandbox.
  • The url parameter is of the incorrect type or format.
  • The call does not have the correct number of parameters.

See also


Example
The following example shows an implementation of the upload() method by first prompting the user to select a file to upload, then handling the onSelect and onCancel listeners, and finally handling the results of the actual file upload.
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 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);

Event detail
onCancelevent listener

public onCancel = function(fileRef:FileReference) {}

Player version: Flash Player 8

Invoked when the user dismisses the file-browsing dialog box. This dialog box is displayed when you call FileReference.browse(), FileReferenceList.browse(), or FileReference.download().

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.

Example
The following example traces a message if the user dismisses the file-browsing dialog box. This method is triggered only if the user clicks Cancel or presses the Escape key after the dialog box is displayed.
import flash.net.FileReference;

var listener:Object = new Object();

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

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
if(!fileRef.download(url, "FlashPlatform.pdf")) {
    trace("dialog box failed to open.");
}

onCompleteevent listener 

public onComplete = function(fileRef:FileReference) {}

Player version: Flash Player 8

Invoked when the upload or download operation has successfully completed. Successful completion means that the entire file has been uploaded or downloaded. For file download, this event listener is invoked when Flash Player has downloaded the entire file to disk. For file upload, this event listener is invoked after the Flash Player has received an HTTP status code of 200 from the server receiving the transmission.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.

Example
The following example traces out a message when the onComplete event is triggered.
import flash.net.FileReference;

var listener:Object = new Object();

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

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
fileRef.download(url, "FlashPlatform.pdf");

onHTTPErrorevent listener 

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

Player version: Flash Player 8

Invoked when an upload fails because of an HTTP error.

Because of the way that Flash Player relies on the browser stack during file download, this error is not applicable for download failures. If a download fails because of an HTTP error, the error is reported as an I/O error.

Parameters
fileRef:FileReference — The File Reference object that initiated the operation.
 
httpError:Number — The HTTP error that caused this upload to fail. For example, an httpError of 404 indicates that a page is not found. HTTP error values can be found in sections 10.4 and 10.5 of the HTTP specification at ftp://ftp.isi.edu/in-notes/rfc2616.txt.

Example
The following example creates a FileReference object with a listener for each possible event including onHttpError. This listener is triggered only if the upload fails because of an HTTP error.
import flash.net.FileReference;

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();

onIOErrorevent listener 

public onIOError = function(fileRef:FileReference) {}

Player version: Flash Player 8

Invoked when an input/output error occurs.

This listener is invoked when the upload or download fails for any of the following reasons:

Important: Only Flash applications that are running in a browser — that is, using the browser plug-in or ActiveX control — can provide a dialog to prompt the user to enter a user name and password for authentication, and then only for downloads. For uploads that use the plug-in or ActiveX control, or that upload and download using either the standalone or external players, the file transfer fails.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.

Example
The following example traces a message when the onIOError event is triggered. For simplicity, none of the other event listeners are included in this example.
import flash.net.FileReference;

var listener:Object = new Object();

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

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
fileRef.download("http://www.adobe.com/NonExistentFile.pdf", "NonExistentFile.pdf");

onOpenevent listener 

public onOpen = function(fileRef:FileReference) {}

Player version: Flash Player 8

Invoked when an upload or download operation starts.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.

Example
The following example traces a message when the onOpen event is triggered.
import flash.net.FileReference;

var listener:Object = new Object();

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

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
fileRef.download(url, "FlashPlatform.pdf");

onProgressevent listener 

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

Player version: Flash Player 8

Invoked periodically during the file upload or download operation. The onProgress listener is invoked while the Flash Player transmits bytes to a server, and it is periodically invoked during the transmission, even if the transmission is ultimately not successful. To determine if and when the file transmission is successful and complete, use onComplete.

In some cases, onProgress listeners are not invoked; for example, if the file being transmitted is very small, or if the upload or download happens very quickly.

File upload progress cannot be determined on Macintosh platforms earlier than OS X 10.3. The onProgress event is called during the upload operation, but the value of the bytesLoaded parameter is -1, indicating that the progress cannot be determined.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.
 
bytesLoaded:Number — The number of bytes transmitted so far.
 
bytesTotal:Number — The total size of the file to be transmitted, in bytes. If the size cannot be determined, the value is -1.

Example
The following example traces the progress of a download using the onProgress event listener.
import flash.net.FileReference;

var listener:Object = new Object();

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

var fileRef:FileReference = new FileReference();
fileRef.addListener(listener);
var url:String = "http://www.adobe.com/platform/whitepapers/platform_overview.pdf";
fileRef.download(url, "FlashPlatform.pdf");

See also

onSecurityErrorevent listener 

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

Player version: Flash Player 8

Invoked when an upload or download fails because of a security error. The calling SWF file may have tried to access a SWF file outside its domain and does not have permission to do so. You can try to remedy this error by using a policy file.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.
 
errorString:String — Describes the error that caused onSecurityError to be called. The value is "securitySandboxError".

Example
The following example creates a FileReference object with a listener for each possible event, including onSecurityError. The onSecurityError listener is triggered only if the upload fails because of a security error.
import flash.net.FileReference;

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();

onSelectevent listener 

public onSelect = function(fileRef:FileReference) {}

Player version: Flash Player 8

Invoked when the user selects a file to upload or download from the file-browsing dialog box. (This dialog box is displayed when you call FileReference.browse(), FileReferenceList.browse(), or FileReference.download().) When the user selects a file and confirms the operation (for example, by clicking OK), the properties of the FileReference object are populated.

The onSelect listener works slightly differently depending on what method invokes it. When onSelect is invoked after a browse() call, Flash Player can read all of the FileReference object's properties, because the file selected by the user is on the local file system. When onSelect is invoked after a download() call, Flash Player can read only the name property, because the file hasn't yet been downloaded to the local file system at the moment onSelect is invoked. When the file has been downloaded and onComplete invoked, then Flash Player can read all other properties of the FileReference object.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.

Example
The following example traces a message within the onSelect event listener.
import flash.net.FileReference;

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.");
    }
}

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

onUploadCompleteDataevent listener 

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

Language version: ActionScript 2.0
Player version: Flash Player 9.0.28.0

Invoked after data is received from the server after a successful upload. This listener is not invoked if data is not returned from the server.

Parameters
fileRef:FileReference — The FileReference object that initiated the operation.
 
data:String — The raw data returned from the server in response to the successful upload.