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 Error Handle

In previous Flash ActionScript download file tutorial, it assumes that the downloding process is correct. Like the Flash AS loading external files, this is always better to let the users know what happen in case an error appear, for example, incorrect file name.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

The Flash ActionScript will display the error message in the text field in case an error happens. This is much better to let the Flash Movie throwing the horror message to the user.

The error checking process is quite similar to the loading external flash movies. In this Flash ActionScript example, we check the errors in two level. The first level checking the IO_ERROR error, the second level checking the SecurityError error and the IllegalOperationError.

Here's the meaning of the errors:

  • The IOError exception is thrown when some type of input or output failure occurs.
  • The SecurityError exception is thrown when some type of security violation takes place.
  • The IllegalOperationError exception is thrown when a method is not implemented or the implementation doesn't cover the current usage.

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("download_file.txt");

 

// 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;

}

 

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-02.fla

Remarks:

Always check errors when loading external assets and downloading files.