User Guide
412 Networking and Communication
Beginning the file download
When the user clicks the Download Button component instance on the Stage, the
startDownload() method is to initiate the file download process. The following excerpt
shows the
startDownload() method:
/**
* Begin downloading the file specified in the DOWNLOAD_URL constant.
*/
public function startDownload():void
{
var request:URLRequest = new URLRequest();
request.url = DOWNLOAD_URL;
fr.download(request);
}
First, the startDownload() method creates a new URLRequest object, and sets the target
URL to the value specified by the
DOWNLOAD_URL variable. Next, the
FileReference.download() method is called, and the newly created URLRequest object is
passed as a parameter. This causes the operating system to display a dialog box on the user’s
computer prompting them to select a location to save the requested document. Once the user
selects a location, the
open event (Event.OPEN) is dispatched and the openHandler()
method is invoked.
The
openHandler() method sets the text format for the ProgressBar component’s label
property, and enables the Cancel button, which allows the user to immediately stop the
download in progress. The
openHandler() method is as follows:
/**
* When the OPEN event has dispatched, change the progress bar's label
* and enable the "Cancel" button, which allows the user to abort the
* download operation.
*/
private function openHandler(event:Event):void
{
pb.label = "DOWNLOADING %3%%";
btn.enabled = true;
}