HTML5 multiple files upload JavaScript example for CFML (ColdFusion, Lucee, Railo) using XMLHttpRequest Level 2.
The HTML5 <input> multiple Attribute is not correctly handled by ColdFusion/Lucee's cffile action = "uploadAll" tag or the FileUploadAll functions. This application uses JavaScript to correct the form file submission without the need of library dependencies such as JQuery.
- ColdFusion, Lucee or CFML compatible server that supports the
cffile action = "uploadAll"tag or theFileUploadAllfunction. - A client web browser that supports HTML5, XMLHttpRequest Level 2 and the FormData API. Can I use XMLHttpRequest 2?
- A client web browser that has JavaScript turned on.
- A client web browser with its JavaScript console enabled to view
console.logoutputs. You can use FireBug in FireFox, DevTools console in Chrome or the Web Inspector console in Safari.
submit.js in its current form is too verbose as it is intended for debugging and feedback. It should not be used in a production environment without modification.
- Download the ZIP
html5Upload-master.zipand extract it into the webroot of your CFML server. - Edit
submit.cfmand make sure thesavedUploadsPathvariable at line 9 points to a valid directory. This is where the file uploads will be saved. - View
upload.htmlin a modern web browser. For a ColdFusion server running on a local machine the URL to do this might be http://localhost:8500/html5Upload-master/upload.html.
The application comes with 3 primary files plus a customized version of BootStrap for some basic HTML5 theming.
upload.htmlA standard HTML5 file upload form. One important difference is it uses a JavaScriptonclickevent to submit the form.submit.cfmA typical CFML page that uses the `cffile action = "uploadAll" to receive and save file uploads to the server.submit.jsIs where the magic happens. It uses XMLHttpRequest to submit the form to the server and handle any responses returned by the server.
When the upload.html Upload files! button is clicked a submit.js startUpload() function is triggered. It has the content of the form passed to it and analyses that content. The function creates a new internal form using the web browser's FormData API and fills it in using the data sourced from the submitted form. Most importantly it creates a new collection of field-names that are compatible with the CFML cffile action = "uploadAll" tag and the FileUploadAll function.
The startUpload() function POSTs the FormData API generated form to submit.cfm using the in-browser XMLHttpRequest() function. submit.cfm treats the POST as a typical form and handles it with the cffile action = "uploadAll" tag. All errors or success messages created by submit.cfm are returned to web browser as HTML tags sent over HTTP. These replies are handled by XMLHttpRequest events contained in submit.js. The events use JavaScript to update the HTML DOM which refresh the client web browser with the updated HTML tags.
Upload files without the html5Upload JavaScript
- New Tricks in XMLHttpRequest2 by Eric Bidelman
- Html5 File Upload with Progress by Shiv Kumar
- w3schools - The XMLHttpRequest Object
- W3C - File API
- W3C - FormData XMLHttpRequest
The MIT License (MIT) Copyright (c) 2015 Ben Garrett



