User Guide
414 Networking and Communication
pb.label = "DOWNLOAD CANCELLED";
btn.enabled = false;
}
First, the code stops the file transfer immediately, preventing any further data from
downloading. Next, the progress bar’s label property is updated to notify the user that the
download has been successfully cancelled. Finally, the Cancel button is disabled, which
prevents the user from clicking the button again until they attempt to download the file again.
Uploading files to a remote server
The file upload process is very similar to the file download process. The FileUpload class
declares the same four variables, as shown in the following code:
private const UPLOAD_URL:String = "http://www.yourdomain.com/
your_upload_script.cfm";
private var fr:FileReference;
private var pb:ProgressBar;
private var btn:Button;
Unlike the FileDownload.DOWNLOAD_URL variable, the UPLOAD_URL variable contains the
URL to the server-side script that will upload the file from the user’s computer. The
remaining three variables behave the same as their counterparts in the FileDownload class.
Initializing the FileUpload component
The FileUpload component contains an init() method, which gets called from the main
application. This method takes two parameters,
pb and btn, which are references to a
ProgressBar and Button component instance on the Stage. Next, the
init() method
initializes the FileReference object defined earlier in the FileUpload class. Finally, the method
assigns four event listeners to the FileReference object. The code for the
init() method is as
follows:
public function init(pb:ProgressBar, btn:Button):void
{
this.pb = pb;
this.btn = btn;
fr = new FileReference();
fr.addEventListener(Event.SELECT, selectHandler);
fr.addEventListener(Event.OPEN, openHandler);
fr.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fr.addEventListener(Event.COMPLETE, completeHandler);
}