Using the HTML5 File API to upload files over AJAX is a great way to to add file upload capabilities and not resort to using a flash based solution or iframe hacks.
It’s easy to use and works with most modern browsers. This example use the Data URI Scheme to transfer the file over AJAX.
The mime type and base64 encoded file are sent as a standard parameter on a POST request. The server then decodes the image and saves it.
Data URI Scheme
An image can be embedded in a HTML or CSS document using this method. The example below is an encoded red dot image. The browser is just reading the embedded image as it’s src attribute (scroll to see the entire URI).
On the client side your going to need to bind a change event to the file attachment field. The change event loops over all the files chosen, creates a FileReader object which uses the readAsDataURL method to encode the file using the Data URI Scheme. An object containing the orignial filename and data is then added to an array.
When the form is submitted each file in the array is read and sent across in an AJAX call.
The AJAX endpoint on the server just has to read the data and reassemble the file. Since we only want the actual file data we need to cut away the mime type and encoding. So we just look for base64, and take everything after it.
If you wanna just checkout a complete working example head on over to Github, clone the repo and run: