Filereference class
The workflow is relative simple: you create a filereference class, call the browse and upload methods and add listener for events while file upload. If all goes fine, you will receive an onComplete event, if not, different errors events. Let's summarize methods and events the class as you can find in ActionScript dictionary:
Method summary
| Signature |
Description |
|---|---|
| addListener |
Registers an object to receive notification when a FileReference event listener is invoked. |
| browse |
Displays a file-browsing dialog box in which the user can select a local file to upload. |
| cancel |
Cancels any ongoing upload or download operation on this FileReference object. |
| download |
Displays a dialog box in which the user can download a file from a remote server. |
| removeListener |
Removes an object from the list of objects that receive event notification messages. |
| upload |
Starts the upload of a file selected by a user to a remote server. |
Event summary
|
Event |
Description |
|---|---|
|
onCancel |
Invoked when the user dismisses the file-browsing dialog box. |
|
onComplete |
Invoked when the upload or download operation has successfully completed. |
|
onHTTPError |
Invoked when an upload fails because of an HTTP error. |
|
onIOError |
Invoked when an input/output error occurs. |
|
onOpen |
Invoked when an upload or download operation starts. |
|
onProgress |
Invoked periodically during the file upload or download operation. |
|
onSecurityError |
Invoked when an upload or download fails because of a security error. |
|
onSelect |
Invoked when the user selects a file to upload or download from the file-browsing dialog box. |
Simple upload
Here's an excerpt of the file upload process:
//Allow this domain
System.security.allowDomain("http://localhost", "127.0.0.1");
import flash.net.FileReference;
// The listener object listens for FileReference events.
var listener:Object = new Object();
listener.onSelect = function(selectedFile:FileReference):Void {
statusArea.text += "Attempting to upload " + selectedFile.name + "\n";
selectedFile.upload("http://localhost/upload/upload.php");
};
// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void {
statusArea.text += "Uploading " + selectedFile.name + "\n";
};
listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "HTTPError number: "+httpError +"\nFile: "+ file.name;
}
listener.onIOError = function(file:FileReference):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "IOError: "+ file.name;
}
listener.onSecurityError = function(file:FileReference, errorString:String):Void {
imagePane.contentPath = "error";
imagePane.content.errorMSG.text = "SecurityError: "+SecurityError+"\nFile: "+ file.name;
}
// the file has uploaded
listener.onComplete = function(selectedFile:FileReference):Void {
statusArea.text += "Upload finished.\nNow downloading " + selectedFile.name + " to player\n";
details.text = ""
for(i in selectedFile) details.text +="<b>"+i+":</b> "+selectedFile[i]+"\n"
downloadImage(selectedFile.name);
};
var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);
At the beginning, we allow the domain (in this case just localhost) trough
System Security object to bypass sandbox violation messages (see Flash
Player 8 security changes), next we setup some of the available listeners
for our imageFile object. The most important are onSelect (a file was selected,
so upload it) and onComplete, where the upload finish. The other handlers deals
with other possible errors (HTTP, I/O, Security) and shows in the scrollPane
we will use to display the loaded image (in the case of failure, of course)
An important thing to take into account is the fact that onProgress
event just return -1 for file upload, so you can't use a bar progress to know
the progress of your uploading.
Note that in the onSelect handler we call the upload process just passing the path to the file in charge of managing the upload, a php file. You can use whatever server side language you want, as far as it can manage uploading. Remember to change path if your path is different.

5 most recent
Flash streaming servers
Tree menu
Flash Spell Checker
Flash Remoting Library
MX 2004 Chart/Poll
Articles