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




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net