ColdFusion And AJAX File Upload

I have created simple jquery plugin for file upload. I have used multiple file upload in Ajax but it’s not possible to upload file in Ajax.

This problem can be solved by this two method.

  1. flash to solve this problem.
  2. JavaScript works.

Javascript works more nice than Flash.

AJAX Upload allow you to easily upload multiple files without refreshing the page
and you can use any element to show file selection window. It works in all major browsers and doesn’t require any library to run. AJAX Upload doesn’t pollute the global namespace, and it’s tested with jQuery, Prototypejs.

The basic principal is that you hi-jack the form submission process and redirect it to
point to a hidden iFrame that you create on the fly. This iFrame then handles the file
upload the same way that any ColdFusion page would handle a file upload. Once the files
are uploaded, you can either return the data, as I do, or just halt processing.

Ok,let’s quickly review the code . Here is our the ColdFusion and AJAX demo :

Demo Download

Please review simple html page: ajaxUpload.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<title>ColdFusion And AJAX File Upload Demo</title>

<!– Linked scripts. –>
<script type="text/javascript" src="/js/jquery-1.2.2.pack.js"></script>
<script type="text/javascript" src="/js/ajax_upload.js"></script>
var browserFields=0;
function funtest(){
function addNew()
browserFields = browserFields + 1;

var tbody = document.getElementById("tblAttach").getElementsByTagName("tbody")[0];
var row = document.createElement("TR");
var td1 = document.createElement("TD");
var td1content = ‘File## ‘ + browserFields + ‘&nbsp;<input type="file" name="upload’ + browserFields + ‘" class="frmitm">’;
document.my_form.browserFields.value = browserFields;
<form id="my_form" name="my_form">
<input type="hidden" name="browserFields">
ColdFusion And AJAX File Upload Demo
<table cellspacing="0" cellpadding="2" border="0">
<table id="tblAttach">
<td align="right"><input type="button" name="browser" value="Add New field" onclick="addNew()" class="but"></td>
<td><input type="button" value="Upload Files" name="test1" id="test1" onclick="funtest();")/></td>



let’s take a look at our ColdFusion page that will handle the AJAX style file uploads:ajaxUpload.cfm


Create an array of files names that we are going to be
passing back to the client.
<cfset arrFiles = [] />

Loop over form fields looking for files. We dont know if
or how many files are going to be uploaded at this point.
<cfloop index="strFileIndex" from="1" to="#form.browserFields#" step="1">

<!— Build dynamic file field (form field key). —>
<cfset strField = "upload#strFileIndex#" />

Check to see if file field exists and that it has
value in it (file path).
<cfif (
StructKeyExists( FORM, strField ) AND
Len( FORM[ strField ] )

<!— Upload file. —>
<cffile action="upload" filefield="#strField#" destination="#Application.path#\upload\"

Add the generated server file name to the array of
file names that we are going to return.
<cfset ArrayAppend( arrFiles, CFFILE.ServerFile ) />



Create the return HTML. Remember, we are going to be
treating the BODY of the returned document as if it
were a JSON string.
<cfsavecontent variable="strHTML">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<body>#SerializeJSON( arrFiles )#</body>


<!— Create binary response data. —>
<cfset binResponse = ToBinary( ToBase64( strHTML ) ) />

<!— Tell the client how much data to expect. —>
value="#ArrayLen( binResponse )#"

Stream the "plain text" back to the client. It’s actually
HTML and it is important that we announce it as HTML
otherwise the client might not know how to work with it.


Here is the jQuery Javascript that hi-jacks the form upload and wires the client and server together: ajax_upload.js


var objData = ”;
// When the DOM loads, initailize the form to upload the files
// using an AJAX "like" call rather than a form submit.
ajaxFileUpload: function(options){
var defaults = {
callback : function(res){}
var opts = $.extend({}, defaults, options);
return this.each(function(){
var jForm = $(this);
var jThis = $(this);
var strName = ("uploader" + (new Date()).getTime());
var jFrame = $("<iframe name=\"" + strName + "\" src=\"about:blank\" />");
jFrame.css("display", "none");
var objUploadBody = window.frames[strName].document.getElementsByTagName("body")[0];
var jBody = $(objUploadBody);
objData = eval("(" + jBody.html() + ")");
//prompt("Return Data:", objData);
}, 100);
jThis.attr("action", "/ami/ajax_upload_action.cfm").attr("method", "post").attr("enctype", "multipart/form-data").attr("encoding", "multipart/form-data").attr("target", strName);


Reference : Clickhere