Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Flash ActionScript Download File with Downloading Progress

In previous Flash ActionScript downloading file tutorial, the user do not know the progress of the download. In other words, the user do not know the status of the download, when the download is completed. In this Flash Actionscript tutorial, the downloading progress is showing to user.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

This Flash ActionScript tutorial is quite similar to the Preloader of loading external files. You may check the Flash ActionScript tutorial loading external image.

Here's how Flash ActionScript shows the file downloading progress to user:

  • The FileReference object will dispatch a PROGRESS event when the file is downloading to user local computer.
  • The FileReference object will dispatch a COMPLETED event when the file download is completed.
Therefore we can use the PROGRESS event and COMPLETED event to add the Listener.

Flash ActionScript Codes:

// Create a new instance of the FileReference class to work with.
var myfileReference:FileReference = new FileReference();

 

// Create a new URLRequest object specifying the location or URL of the download file.
// URL
// var myRequest:URLRequest = new URLRequest("http://domain.com/index.html");
// relative location
var myRequest:URLRequest = new URLRequest("downloadFile.zip");

 

// 1st level IO_ERROR input and output error checking
// Listen error events for the loading process
myfileReference.addEventListener(IOErrorEvent.IO_ERROR, ioError);

 

function ioError(event:ErrorEvent):void {

// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is an IO error during the file downloading. The error is:" + "\n" + event;

}

 

// Listen to when Save button has been clicked on
myfileReference.addEventListener(Event.SELECT, fileSelected);

 

function fileSelected(evt:Event):void {

output_txt.text = "File " + evt.target.name + " has been selected for downloading";
}

 

// Listen to when Cancel button has been clicked on
myfileReference.addEventListener(Event.CANCEL, fileCancelled);

 

function fileCancelled(evt:Event):void {

output_txt.text = "File has been cancelled for downloading";

}

 

// Listen to the file downloading progress
myfileReference.addEventListener(ProgressEvent.PROGRESS, fileDownloadProgress);

 

function fileDownloadProgress(evt:ProgressEvent):void {

// Show file size
output2_txt.text = "File size: " + evt.bytesTotal + " Bytes";

//Calculate the loading percentage
//Declare the loading percentage
var percent:Number = Math.round((evt.bytesLoaded/evt.bytesTotal) * 100);

//Display the loading percentage to visitors
output_txt.htmlText = "Downloading file.... " + String(percent) + "% loaded";
}

 

// Listen to when file downloading is completed
myfileReference.addEventListener(Event.COMPLETE, fileDownloadCompleted);

 

function fileDownloadCompleted(evt:Event):void {

output_txt.htmlText = output_txt.text + "<BR>" + "<font color='#ff0000'>FILE DOWNLOAD COMPLETED!</font>";

}

 

function downloadFile (event:MouseEvent):void {

 

// 2nd level SecurityError error checking
// Use the try-catch block
try {

// Call the download method with the URLRequest object as the parameter.
myfileReference.download(myRequest);

} catch (error:SecurityError) {

// catch the error here if any
// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is a Security error during the file downloading. The error is:" + "\n" + error;

} catch (error:IllegalOperationError) {

// catch the error here if any
// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is an operation error during the file downloading. The error is:" + "\n" + error;

}



}

// Hook up the button with the function downloadFile
download_btn.addEventListener(MouseEvent.CLICK, downloadFile);

Download Flash Source File:

Flash Source File as3-download-file-04.fla

Remarks:

As you can see this Flash ActionScript is quite similar to the Preloader of external files.