Posts Tagged ‘Web’

This code shows the most basic functions to build a Flash photo-uploader.
A single File Uploader demonstrating file size & file type limitations with “uploading progress” check.

The live demo, JavaScript, PHP codes, and EXTRA features of this solution can be found at http://demo.programmerspride.com/flash-upload-resize-photo/

import flash.net.FileReference;
var imageFile:FileReference = new FileReference();
var listener:Object = new Object(); // The listener object listens for FileReference events
imageFile.addListener(listener);
var upload_file:Boolean;

// When button pressed to browse a File
btn_browse.onPress = function():Void {
	// Actual browsing of Files
	imageFile.browse([{description: "Image Files (*.jpg;*.JPG;*.gif;*.GIF;*.png;*.PNG)",
					   extension: "*.jpg;*.JPG;*.gif;*.GIF;*.png;*.PNG"}]);
}

// Upload the File
btn_upload.onPress = function():Void {

	if(upload_file) {

		imageFile.upload("upload.php?sfn="+ _root.session_id +"_"+ _root.filename);
	} else getURL("javascript:img.empty()");
}

// When done selecting a File
listener.onSelect = function ():Void {

	var bytes = imageFile.size;
	var unit = "B";

	if(bytes >= 1073741824) {

		bytes = (bytes / 1024 / 1024 / 1024);
		unit = "GB";
	}
	else
	if(bytes >= 1048576) {

		bytes = (bytes / 1024 / 1024);
		unit = "MB";
	}
	else
	if(bytes >= 1024) {

		bytes = (bytes / 1024);
		unit = "KB";
	}

	if(unit != "B") {

		// Round it off in 2 decimal places
		bytes = (Math.round(bytes * Math.pow(10, 2)) / Math.pow(10, 2))
	}

	if(bytes > 5 && (unit == "MB" || unit == "GB")) upload_file = false;
	else upload_file = true;

	getURL("javascript:img.fileinfo('"+ bytes +"_"+ unit + '|' + imageFile.name +"');");
}

listener.onProgress = function (imageFile:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {

	var p = Math.round((bytesLoaded / bytesTotal) * 100);
	getURL("javascript:img.progress('"+ p +"');");
}

listener.onComplete = function():Void {

	getURL("javascript:img.uploaded('"+ _root.session_id +"_"+ _root.filename +"');");
}

For file checking and debugging purposes, you can include the following lines:

listener.onHTTPError = function(imageFile:FileReference):Void {

	trace("onHTTPError: "+ imageFile.name);
}

listener.onIOError = function(imageFile:FileReference):Void {

	trace("onIOError: "+ imageFile.name);
}

listener.onSecurityError = function(imageFile:FileReference, errorString:String):Void {

	trace("onSecurityError: "+ imageFile.name +" errorString: "+ errorString);
}