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 - A Basic Example

This Flash ActionScript upload files process seems to be good up to now. This is a good idea to test upload a file. Please note that this is just a very simple and basic of Flash Actionscript uploading file application. Some monitoring processes are neglected.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Same as the downloading files process, the Flash ActionScript will dispatch different events depending on which button has been clicked on:

  • Create a new URLRequest instance sending data to "upload.php". Please note that the upload.php will handle the uploading file process, not the Flash ActionScript.
  • Call the upload( ) method of FileReference object to upload a file. Then the data of the upload file will then be sent to the PHP file to process.

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 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();


}

 

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

 

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

Remarks:

Up to now, the Flash ActionScript codes can upload files from user local disk to web server. This is just a very simple example showing how to upload files with Flash ActionScript.