Tuesday, January 22, 2008

Uploading Files with AS3

     I am currently working on a project that contains a lot of upload functionality from within Flash. Running a basic upload routine to choose a file and send it to a server script is easy and well-documented. However, what I needed, was to upload a file to a script, and then receive back a url to that uploaded file. Turns out this is a piece of cake with AS3.
 
     The FileReference class has many events. These include events for the beginning of the upload, io errors, cancellations, upload progress, and the completion of the upload. What I originally tried to do was listen to Event.COMPLETE. The problem with this is that, while the event fires when the file has uploaded, it returns no information about that file. The answer lies in the DataEvent.UPLOAD_COMPLETE_DATA event. When the DataEvent is fired, you can access the path to the file you uploaded by referencing the event's data property. 
 
     I've put together a simple example that demonstrates how to do this. You can download the file here.

1 comments:

Sveinung said...

Doh, link is nor working