HTML5 - code example of File API - drag and drop hard drive files to a webpage! | | ‎ | 33 Comments ‎ | 107,744 Views Geolocation,

In previous blog entries, I shared HTML5 code examples for drag & drop contenteditable and localStorage . Today, I will share a simple app that provides a code example of FileReader , which is part of the File API specification of HTML5. As in my previous HTML5 examples, I'm seeking to create a simple but useful demonstration that exercises HTML5 in a novel way. My goal is to not only demonstrate the HTML5 API, but to give examples on how a developer may actually implement the API in a useful and innovative manner. Accessing the local file system from a web page has been greatly simplified in HTML5, using the File API. The File specification provides an API for representing file objects in web applications, as well as programmatically selecting them and accessing their data.

The File API includes:

FileList sequence, which represents an array of individually selected files from the underlying system. The user interface for selection can be invoked via <input type="file">, i.e. when the input element [HTML5] is in the File Upload state. Blob interface, which represents raw binary data, and allows access to ranges of bytes within the Blob object. File interface, which includes readonly informational attributes about a file such as its name, its mediatype, and a URL to access its data. FileReader interface, which provides methods to read a File, and an event model to obtain the results of these reads. FileError interface and a FileException exception which define error conditions used by this specification.

How the demo works: For my example, I have provided a palette from which to drag & drop into, or alternatively use the File chooser, to select image files off your local file system. For this example, please select image files only, as I have not built in any filtering or error detection for non-graphical files. Remember, with HTML5 not fully implemented in all browsers, this sample will require an HTML5 compliant browser, such as Firefox 3.5 or above.



Here is my example (drag & drop or choose a image to add to the palette):





The key methods to implement the File APIs are basic and easily implemented as follows:

function imagesSelected(myFiles) {

for (var i = 0, f; f = myFiles[i]; i++) {

var imageReader = new FileReader();

imageReader.onload = (function(aFile) {

return function(e) {

var span = document.createElement('span');

span.innerHTML = ['<img class="images" src="', e.target.result,'" title="', aFile.name, '"/>'].join('');

document.getElementById('thumbs').insertBefore(span, null);

};

})(f);

imageReader.readAsDataURL(f);

}

}



function dropIt(e) {

imagesSelected(e.dataTransfer.files);

e.stopPropagation();

e.preventDefault();

}







I chose the < td> element for my ondrop event:



<td align="left" height="105" ondragenter="return false" ondragover="return false" ondrop="dropIt(event)">

<output id="thumbs"></output>

</td>



You can download the complete HTML of this example by clicking here.

In this example, I only drop the local file image (in thumbnail format) onto a palette. However, I think this demonstrates the simplicity of the File API, and its potential.

