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 Response to Save and Cancel Button

In a practical Flash ActionScript downloading file application, this is better to know whether the user click on the Save button or Cancel button. In stead of clicking on the Save button, the user may choose click on the Cancel button not to download the file.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

The Flash ActionScript will dispatch different events depending on which button has been clicked on:

  • The FileReference object will dispatch a SELECT event when the Save button has been clicked.
  • The FileReference object will dispatch a CANCEL event when the Cancel button has been clicked.
Therefore we can use the SELECT event and CANCEL 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("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;

}

 

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

 

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

Remarks:

This is a good Flash ActionScript practise to response to user click on Save or Cancel button.