hide side navigation
    5 most recent
    Web Services
    Library's
    Component's
    Applications
    Articles
  Flash button as Flex icon  Flex form by email  Hello Remoting with AS3  AS3 Saving data from Flash  AS3 Loading data into Flash  Fire Effect  Contact form  Dragable buttons  Hello World with openamf  Loading helper classes  Upload with Flash 8  Transitions effects  Snapshot with Flash 8  Hello World Remoting AS2  Flash AS2 Remoting Connector  Saving data from Flash  Loading data into Flash  FlashCom & Remoting login  Cell Renderer API  Editing a table using remoting components  Flash MX2004 web service classes  Browsing a catalog  amfphp Documentation  Hello World Remoting  Online Store with AMFPHP  Flash clients for Web Services  Web Service Walk Though with NuSoap  Popup windows in flash with javascript  Installing Apache/PHP  MoreOver News Feeds  Load Edit Save Text Files via CGI  Save Movie Clip Postion via PHP and MySQL
Current Page (1) Next Page >>  View Article Example >> 1 | 2

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(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) : Boolean

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

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.

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.

Current Page (1) Next Page >> 1 | 2