User Guide

Example: Uploading and downloading files 413
Monitoring a file’s download progress
As a file downloads from a remote server to the user’s computer, the progress event
(
ProgressEvent.PROGRESS) is dispatched at regular intervals. Whenever the progress event
is dispatched, the
progressHandler() method is invoked and the ProgressBar component
instance on the Stage is updated. The code for the
progressHandler() method is as follows:
/**
* While the file is downloading, update the progress bar's status.
*/
private function progressHandler(event:ProgressEvent):void
{
pb.setProgress(event.bytesLoaded, event.bytesTotal);
}
The progress event contains two properties, bytesLoaded and bytesTotal, which are used to
update the ProgressBar component on the Stage. This gives the user a sense of how much of
the file has already finished downloading and how much remains. The user can abort the file
transfer at any time by clicking the Cancel button below the progress bar.
If the file is downloaded successfully, the
complete event (Event.COMPLETE) invokes the
completeHandler() method, which notifies the user that the file has completed
downloading and disables the Cancel button. The code for the
completeHandler() method
is as follows:
/**
* Once the download has completed, change the progress bar's label one
* last time and disable the "Cancel" button since the download is
* already completed.
*/
private function completeHandler(event:Event):void
{
pb.label = "DOWNLOAD COMPLETE";
btn.enabled = false;
}
Cancelling a file download
A user can abort a file transfer and stop any further bytes from being downloaded at any time
by clicking the Cancel button on the Stage. The following excerpt shows the code for
cancelling a download:
/**
* Cancel the current file download.
*/
public function cancelDownload():void
{
fr.cancel();