# Quick Tip: Download Files Via SWFs Using FileReference

This Quick Tip covers how to use AS3's FileReference class in order to download and save external files from Flash RIAs without the need for server side scripts like PHP. All we need is the path of the file that we want to let the user download.

## Final Result Preview

Let's take a look at the final result we will be working towards:

## Step 1: Create a New ActionScript 3.0 File

Create a new .fla file and save it into your project folder.

## Step 2: Prepare the UI

For this Quick Tip, I created an interface. You can download it from the link at the top of the page or you can create your own.

In my .fla file, there are three images which represent the file types, and three download buttons which are MovieClip Objects

• btn_img_download for the miki-monk.jpg file
• btn_mp3_download for the some-audio.mp3 file
• btn_txt_download for the dummy-text.rtf file

and a progress bar at the bottom to track the download progress placed on the Stage.

## Step 3: Create Document Class

We will write our code into a Document Class file. If you are not familiar with Document Class, you can get related information from another ActiveTuts+ Quick Tip.

Let's create our Document Class file; click File>New then select "ActionScript 3.0 Class. Save the AS file into the same location as your .fla file.

Link your .fla file to the Action Script File that you created -- simply write the name of your AS file into the related field in your .fla file.

## Step 4: Using FileReference() in Our Code

Here is the Document Class that I used in this Quick Tip. Please read the comments in the code to understand the class behavior.

As you can see the key is to use FileReference with a URLRequest, to enable downloading files from our server. Basically we need 3 things:

1. Create an instance of FileReference Class
2. Create an instance of URLRequest Class
3. Assign the file path to the url parameter of URLRequest instance and call download method of FileReference Class

Note: If you want to download files from another host, you have to put a crossdomain.xml file into that host. Let's say, you put your swf file into www.host-a.com, and you want to download files from www.host-b.com with your swf file in www.host-a.com. To do that, you need permisson from www.host-b.com. So, you have to upload a crossdomain.xml file into www.host-b.com. If you wawnt learn more about crossdomain.xml files, there is another nice Quick Tip here.

## Conclusion

In this Quick Tip, we learned how to download files from a server, via Flash, to the local system without any need for server side scripts like PHP. Hope you like this Quick Tip and thank you for reading it. If you have any questions, please drop a comment below.