Posts Tagged ‘JQUERY’

This is the source code of the “batch-after-batch” multiple file uploader in Flash 8 (ActionScript 2.0) demonstrated at http://demo.programmerspride.com/flash-batch-after-batch-multi-uploadlive demo.

flash multiple file uploader

import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.external.ExternalInterface;

var imageFiles:FileReferenceList = new FileReferenceList;
var list:Array = new Array(); // Handler to the selected files.
var listener:Object = new Object(); // The listener object that listens for FileReference events.
var upload_listener:Object = new Object();
imageFiles.addListener(listener);

// These next four (4) lines will just load the external "browse button".
// Parent movie clip.
mc.createEmptyMovieClip("img", getNextHighestDepth());

// Child movie clip that the image will replace.
mc.img.createEmptyMovieClip("image", 99);

// Create the image loader that loads the "browse button" as configured.
var loader:MovieClipLoader = new MovieClipLoader();
loader.loadClip(_root.btn_browse_name, mc.img.image);

// When the "browse button" is clicked.
mc.onPress = function():Void {
	
	// Actual browsing of Files
	imageFiles.browse([{
		description: _root.file_description,
		extension: _root.file_extension
	}]);
}

// When files are selected then call JS UI for enqueueing.
listener.onSelect = function():Void {
	
	list = imageFiles.fileList;
	var total:Number = list.length;
	
	if(total > 0) {
		
		ExternalInterface.call("img.clearqueue()");
		for(var i:Number = 0; i<total; i++) {
			
			// 0 = Size, 1 = True/False when accepted or not.
			var info:Array = getFileSize(list[i].size); 
			
			// Send filename to JS for queueing.
			ExternalInterface.call("img.enqueue('"+ list[i].name +"','"+ info[0] +"','"+
									info[1] +"')");
		}
	}
}

// Waits for some JS commands from the web-page document.
ExternalInterface.addCallback("upload", this, doFileUpload);
ExternalInterface.addCallback("cancel", this, doFileCancel);

function doFileUpload():Void {
	var total:Number = list.length;
	
	if(total > 0) {
		
		for(var i:Number = 0; i<total; i++) {
							
			// 0 = Size, 1 = True/False when accepted or not.
			var info:Array = getFileSize(list[i].size); 
			if(info[1] == true) { 
		
				list[i].addListener(upload_listener);
				list[i].upload("upload.php?sessid="+ _root.session_id);
			}
		}
		
	} else ExternalInterface.call("img.empty()");
}

// This function is triggered when an item is cancelled by User.
function doFileCancel(filename:String):Void {
	
	doRemoval(filename);
	ExternalInterface.call("img.cancelled('"+ filename +"','"+ ((list.length == 0) ? 1 : 0) +"')");
}

function doRemoval(filename:String) {
	
	var index:Number = getFileIndex(filename);
	
	list[index].cancel(); // Actual cancelling from the memory queue.
	list[index].removeListener(upload_listener); // Remove attached process handlers.
	
	list.splice(index, 1); // Dont' use .pop() in removing an item. Use .splice() instead.
}

upload_listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
	
	var p:Number = Math.round((bytesLoaded / bytesTotal) * 100);
	var tmp:Array = getFileSize(bytesLoaded);
	
	ExternalInterface.call("img.progress('"+ p +"','"+ file.name +"','"+ tmp[0] +"')");
};

upload_listener.onComplete = function(file:FileReference):Void {
	
	var filename:String = file.name;
	doRemoval(filename);
	ExternalInterface.call("img.uploaded('"+ filename +"','"+ ((list.length == 0) ? 1 : 0) +"')");
};

upload_listener.onHTTPError = function(file:FileReference, httpError:Number):Void {

	ExternalInterface.call("this.onError('"+ file.name +"','HTTP Error: "+ httpError +"')");
}

upload_listener.onIOError = function(file:FileReference):Void {

	ExternalInterface.call("this.onError('"+ file.name +"','I/O Error')");
}

upload_listener.onSecurityError = function(file:FileReference, errorString:String):Void {

	ExternalInterface.call("this.onError('"+ file.name +"','Security Error: "+ errorString +"')");
}

Initializing the custom “browse” button:

Stage.scaleMode = "noscale";

var canvas_width:Number = 250;
var canvas_height:Number = 50;

if(_root.btn_browse_width > canvas_width) canvas_width = _root.btn_browse_width;
if(_root.btn_browse_height > canvas_height) canvas_height = _root.btn_browse_height;

var xPosition:Number = ((canvas_width - _root.btn_browse_width) / 2);
var yPosition:Number = ((canvas_height - _root.btn_browse_height) / 2);

mc._x = xPosition;
mc._y = yPosition;

Miscellaneous:

function getFileIndex(filename:String):Number {
	
	var total:Number = list.length;
	if(total > 0) { 
		for(var i:Number = 0; i<total; i++) { 
			if(list[i].name == filename) { 
			
				return i;
			}
		}
	}
	
	return -1;
}

function getFileSize(filesize:Number):Array {
	
	var bytes:Number = filesize;
	var unit:String = "B";
	var accept:Boolean = true;
	
	if(bytes < 1073741824) { // If less than GB
		
		// Means MB ( 1024 * 1024 )
		if(bytes >= 1048576) { // If greater or equal to MB but less than GB
			
			bytes = (bytes / 1024 / 1024);
			unit = "MB";		
		}
		else {
			
			// Means KB ( 1024 )
			if(bytes >= 1024) { // If greater or equal to KB but less than MB
				
				bytes = (bytes / 1024);
				unit = "KB";		
			}			
		}
	}
	else {
		
		// Too large.
		accept = false;
	}
	
	if(unit != "B") {

		// Round it off in 2 decimal places
		bytes = (Math.round(bytes * Math.pow(10, 2)) / Math.pow(10, 2))
	}
	
	if(bytes > _root.file_size_max && unit == "MB") accept = false;
	
	return [bytes +" "+ unit, accept];
	
}

Creating your own Verification Code or some may refer it as Security Code or CAPTCHA is fast and
easy with PHP & GD Library.

Put this solution specially on your “Registration Forms” and other “Web Forms” that may not
require a registration but request an INPUT from Guests and Visitors.

Why bother installing a third-party CAPTCHA when you can build your own while having a full access
and control unto it?

The full PHP source codes and live demo of this solution can be found at:
http://demo.programmerspride.com/verification-code/

verification code

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