Monday, August 2, 2010

Working with files in Actionscript 3: Part 1

In this tutorial we will learn how to let users download files onto their hard drive through flash using the download() method.

The flash.netFileReference class has a download() method that can be used to download files to user's hard drive through a specified URL.The first thing we have to do is to create a File Reference object:

var fileReference:FileReference = new FileReference();

Next we give Flash the URL our file is located at. It can be either relative or absolute.

var urlRequest:URLRequest = new URLRequest("");;

This will open a new dialog window that asks user where does he want to save the file. The user is allowed to change the name of the file when saving it too. By default, the name is the same as the file on your server, however if it is generated by a script, the file might have a long and weird name, for example "H435F6478F88991.jpg". We can change the default file name by entering the second optional parameter for the download() method:

var urlRequest:URLRequest = new URLRequest("");, "yourfile.txt");

When working with download() method, some errors might occur. The most often ones are IllegalOperationError and SecurityError. IllegalOperationError occurs when download() method is called when the dialog window is already opened (only 1 window can be opened at the same time). SecurityError occurs, when SWF doesn't have rights to download the file. You can use your own codes to workout the errors or tell the user about the error, use try...catch method:

try {, "yourfile.txt")
catch (illegalOperation:IllegalOperationError){
// your code
catch (security:SecurityError) {
// your code

Thanks for reading!


Working with files in Actionscript 3: Part 2
Working with files in Actionscript 3: Part 3
Working with files in Actionscript 3: Part 4
Working with files in Actionscript 3: Part 5

1 comment:

Bastian said...

Good explication.
But it lacks the option to download files FLA ...

Post a Comment