User Guide
410 Networking and Communication
This code places a ProgressBar component instance and two Button component button
instances on the Stage. When the user clicks the Upload button (
startUpload), an operating
system dialog box is launched that allows the user to select a file to upload to a remote server.
The other button,
cancelUpload, is disabled by default, although when a user begins a file
upload, the button becomes enabled and allows the user to abort the file transfer at any time.
The code for the Download File panel is as follows:
<mx:Panel title="Download File" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:ProgressBar id="downloadProgress" label="" mode="manual" />
<mx:ControlBar horizontalAlign="right">
<mx:Button id="startDownload" label="Download..."
click="fileDownload.startDownload();" />
<mx:Button id="cancelDownload" label="Cancel"
click="fileDownload.cancelDownload();" enabled="false" />
</mx:ControlBar>
</mx:Panel>
This code is very similar to the file upload code. When the user clicks the Download button,
(
startDownload), the FileDownload.startDownload() method is called, which begins
downloading the file specified in the
FileDownload.DOWNLOAD_URL variable. As the file
downloads, the progress bar updates, showing what percentage of the file has downloaded.
The user can cancel the download at any point by clicking the
cancelDownload button,
which immediately stops the file download in progress.
Downloading files from a remote server
Downloading of files from a remote server is handled by the flash.net.FileReference class and
the custom com.example.programmingas3.fileio.FileDownload class. When the user clicks
the Download button, Flash Player begins to download the file specified in the FileDownload
class’s
DOWNLOAD_URL variable.
The FileDownload class begins by defining four variables within the
com.example.programmingas3.fileio package, the following code shows:
/**
* Hard-code the URL of file to download to user's computer.
*/
private const DOWNLOAD_URL:String = "http://www.yourdomain.com/
file_to_download.zip";
/**
* Create a FileReference instance to handle the file download.
*/
private var fr:FileReference;