Skip to content

Instantly share code, notes, and snippets.

@blenderous
Created December 17, 2016 10:55
Show Gist options
  • Save blenderous/358762ae4c071aff14af130708871507 to your computer and use it in GitHub Desktop.
Save blenderous/358762ae4c071aff14af130708871507 to your computer and use it in GitHub Desktop.
Code to read the file uploaded to a page (using FileReader object)
// file input
var input = document.getElementById('fileinput');
// treatment when file is selected
var handleFiles = function () {
// log the size of the file
console.log('Size of the file is ' + this.files[0].size/1000 + ' kB');
// instantiate a new FileReader object
var fr = new FileReader();
// loading files from the file system is an asynchronous
// operation, run this function when the loading process
// is complete
fr.addEventListener('loadend', function() {
// send the file over web sockets
// ws.send(fr.result);
console.log('end of loading');
});
// load the file into an array buffer
fr.readAsArrayBuffer(this.files[0]);
};
// applying treatment to input
input.addEventListener('change', handleFiles, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment