Previous Page
Next Page

Recipe 23.3. Monitoring Download Progress

Problem

You want to monitor download progress.

Solution

Listen for the progress event.

Discussion

You can monitor the progress of a file(s) as it downloads by using the progress event. Every time part of the file downloads to the user's computer, the FileReference object dispatches a progress event of type ProgressEvent. You can use the ProgressEvent.PROGRESS constant to add a listener, as follows:

fileReference.addEventListener(ProgressEvent.PROGRESS, onFileProgress);

The progress event object has two properties, bytesLoaded and bytesTotal, which return the bytes that have downloaded and the total bytes, respectively. The following example method uses the values of those properties to display the download progress in a text field called fileProgressField:

private function onFileProgress(event:ProgressEvent):void {
    fileProgressField.text = event.bytesLoaded + " of " + event.bytesTotal + " bytes";
}

When the file has completed downloading, the FileReference object dispatches a complete event of type Event. Use the Event.COMPLETE constant to add a listener:

fileReference.addEventListener(Event.COMPLETE, onFileComplete);

See Also

Recipe 23.1


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)