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 Uploading File - Check if Upload File Process Success or Fail

In previous Flash ActionScript uploading file tutorials, we always assume that the upload file process is successful. Therefore we do not take care of the data send back from the PHP file. If we look at the PHP upload file again, it will send back "OK" if the upload file is success and send back "FAIL" if the upload is fail.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Always monitor and let user know if the uploading file is success or not.

PHP File Codes:

There are many free PHP uploading files codes on the Internet, find a simple one suitable for you.

<?php

/ Variable (name) sent from Flash
$filename = $_FILES['Filedata']['name'];

// the uploaded file save as a temporary file on the web server
$filetmpname = $_FILES['Filedata']['tmp_name'];

/* You can return the File Type and File Size back to Flash if you like*/
//$fileType = $_FILES["Filedata"]["type"];
//$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);

// Path the file will be moved to
$destination_path = "upload/";

// Path and file the file will be moved to
$destination_path = $destination_path . $filename;

// Call the move_uploaded_file function to move the file
// The move_uploaded_file function requires two parameters:
// - path of the temporary file
// - path where the file will be moved to
if ( move_uploaded_file($filetmpname, $destination_path) ) {
// move_uploaded_file returns true if the uploaded file was moved successfully,
echo "OK";
} else {
echo "FAIL";
}

?>

Flash ActionScript Codes:

// Set text field to empty
// This use to detect user select file or not
output_txt.text="";

 

// Create a new URLRequest instance sending data to "upload.php"
var myRequest:URLRequest = new URLRequest("upload.php");

 

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

 

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

 

function fileSelected(evt:Event):void {

output_txt.text = "File selected upload: " + evt.target.name;
}

 

function browseFile (event:MouseEvent):void {

output2_txt.text = "";

 

// Use browse( ) method of a FileReference object to open a
// dialog box to browse local disks for a file.
myfileReference.browse();

}

 

// Hook up the button with the function browseFile
browse_btn.addEventListener(MouseEvent.CLICK, browseFile);

 

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

 

// Create a new URLRequest instance sending data to "upload.php"
var myRequest:URLRequest = new URLRequest("upload.php");

 

function fileUploadProgress(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, fileUploadCompleted);

function fileUploadCompleted(evt:DataEvent):void {

// evt.data is the data return from PHP
if (evt.data == "OK") {
output_txt.htmlText = output_txt.text + "<BR>" + "<font color='#ff0000'>FILE DOWNLOAD COMPLETED!</font>";
} else if (evt.data == "FAIL") {
output_txt.htmlText = output_txt.text + "<BR>" + "<font color='#ff0000'>FILE DOWNLOAD FAIL!</font>";
}

}


function uploadFile (event:MouseEvent):void {

// Reset the Alert text field to empty
output2_txt.text ="";

if (output_txt.text.length > 0) {

// Call the upload( ) method of FileReference object to upload a file
myfileReference.upload(myRequest);

} else {

// Alert user to select a file to upload
output2_txt.text = "Please browse for file to upload!";
}

}

// Hook up the button with the function browseFile
browse_btn.addEventListener(MouseEvent.CLICK, browseFile);

Download Flash Source File:

Flash Source File as3-upload-file-06.fla

Remarks:

This Flash ActionScript uploading file shows how to use the data return from PHP file to monitor if the upload file process is success or fail.