Upload file

Demo :

output

Alternative content

Get Adobe Flash player

In this example we have two parts :

  • The flash side which provides to upload a file
  • And the php side which provides to save the file in a directory (on your FTP server for example)

We have to use the class FileReference. (Adobe livedocs, flash API).

package;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileReference;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.net.URLRequest;

class Main { 
	static function main() {
		new Main();
	}
	public function new()
	{
		/*The FileReference class provides a means to upload and download files between a user's computer and a server.:
		 * An operating-system dialog box prompts the user to select a file to upload (or a location for download).
		 * Each FileReference object refers to a single file on the user's disk and has properties that contain 
		 * information about the file's size, type, name, creation date, modification date, and creator type (Macintosh only). 
		 * 
		 * for more details read the Adobe livedocs http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/FileReference.html
		 * */
		var file     = new FileReference();
		
		/*A simple Textfield that displays some informations*/
		var log      = new TextField();
		
		/*The php script which saves the file in a predefined directory*/
		var url      = "http://razaina.fr/tutoriaux/UploadFiles/src/upload.php";
		
		/*these textfields are used as buttons in this example
		 * Event listeners are added on theme later*/
		var choose   = new TextField();
		var send     = new TextField();
 
		/*prepare each elements*/
		choose.text       = "Click here to choose a file";
		choose.border     = true;
		choose.borderColor= 0x999999;
		choose.selectable = true; 
		choose.width      = 200;
		choose.height     = 20;
		choose.background = true;
		choose.backgroundColor = 0xE0E0E0;

		send.text       = " Send file";
		send.border     = true;
		send.borderColor= 0x999999;
		send.selectable = false;
		send.x          = 130; 
		send.width      = 80;
		send.height     = 20;
		send.background = true;
		send.backgroundColor = 0xCCCCCC;
		
		log.text       = "File logs :"; 
		log.y          = 30;
		log.width      = 480;
		log.height     = 300; 
 
		/*prompt an operating-system dialog box and browse file */
		var chooseFile = function(e:MouseEvent)
		{
			file.browse(); 
		}
		/*send file*/
		var sendFile   = function(e:MouseEvent)
		{
			var request = new URLRequest( url ); 
			file.upload(request,"FILE",true);
		}
		
		/*display some informations about the file selected*/
		var select     = function(e:Event)
		{ 
			log.text   =   "File selected !\n"
					   + "File name  : " + file.name + "\n"
				       + "File size  : " + file.size + " octets\n"
				       + "Modification date    : " + file.modificationDate + "\n"; 
		}
		/*If cancelled*/
		var cancel     = function(e:Event)
		{ 
			log.text   += "\n Upload canceled !"; 
		};
		/*If an error occured*/
		var onError    = function(e:IOErrorEvent)
		{
			log.text   += "\n An error occured : "+e.text;
		} 
		/*If file is being uploaded*/
		var onOpen     = function(e:Event)
		{
			log.text   += "\n Uploading...";
	    }
	    /*display the number of bytes loaded compared to number of bytes total to load*/
	    var onProgress = function(e:ProgressEvent)
	    {
			log.text   += " \n Progress : " + e.bytesLoaded + " / " + e.bytesTotal;
		}
		/*upload complete*/
		var onComplete = function(e:Event)
		{ 
			log.text   += "\n Upload is over !";
		}
		
		/*Some event listeners*/
		file.addEventListener("select", select);
		file.addEventListener(Event.CANCEL, cancel);
		file.addEventListener(IOErrorEvent.IO_ERROR, onError); 		
		file.addEventListener(Event.OPEN, onOpen);
		file.addEventListener(ProgressEvent.PROGRESS, onProgress);
		file.addEventListener(Event.COMPLETE, onComplete);

		choose.addEventListener(flash.events.MouseEvent.CLICK, chooseFile);
		send.addEventListener(flash.events.MouseEvent.CLICK, sendFile); 

		/*Add elements on the scene*/
		flash.Lib.current.addChild(log);  
		flash.Lib.current.addChild(choose);
		flash.Lib.current.addChild(send);
	}

}

ok let's write the php script which saves the file in a directory. We're going to use the php.Web.parseMultipart() method to parse multipart datas :

package;
import php.Web;
import php.io.FileOutput;
import php.io.File;
import haxe.io.Bytes;

class Upload
{
	static function main(){ new Upload();}
	public function new()
	{
		/*The current file name*/
		var currentFileName = "";
		
		/*The file output*/
		var output:FileOutput = null;
		var filename = "";
			
		/*onPart() function invoked each time a new part is encountered*/
		var onPart = function(partName:String, fileName:String) {
				/*if there are more than one file uploaded at once, this will close the previous stream*/
				if(null!=output) output.close(); 
				
				filename = fileName; 
				output = File.write ("upload/" + filename, true); 
			};
		/*onData() function called once or more time and depends on the size of the file and the buffer used.*/
		var onData = function (data:String, pos:Int, length:Int)
			{
				currentFileName = (currentFileName != filename)?filename:currentFileName;
				
				/*Write in the file*/
				output.write (Bytes.ofString(data));				
			};
			
		/*call parseMultipart*/			
		Web.parseMultipart (onPart, onData);
		if(null!=output) output.close(); 
		
	} 
}

Compile both files :

haxe -php ./ -main Upload 
haxe -swf9 UploadFiles.swf -main Main
(24 times)