Access file content from browser #javascript #js #nodejs

Oscar Brito


On my latest contribution to opensubtitles.org I needed to access the file's bytes to calculate an HASH value from the browser.

To access the file's content you need to get a dataTranfer object like:



$('#search_field').bind('drop', function(e, ev) {
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;
$.each(files, function(index, file) {

});
});



Then you can read the file's content with:



var reader = new FileReader();

// async callback to get file content
reader.onload = function(e){

// create a DataView for easy iteration
var view = new DataView(e.currentTarget.result);

// get little-endian number (32bits)
var n = view.getUint32(i, true);

};

// get the first 4 bytes
var blob = file.slice(0, 4);
// read the content from the file
reader.readAsArrayBuffer(blob);


The DataTranfer object is used to hold the date of the object being dragged. You can search further on:

More on this on the coming weeks...





Visit www.divhide.com for more informations, contacts and news about Web Development.
See other blog posts at blog.divhide.com.



Divhide purpose is to follow the HTML5 movement and contribute with applications which prove the quality of technology.


Feel free to contact divhide.