Skip to content

Instantly share code, notes, and snippets.

@davidbarredo
Created November 15, 2013 08:22
Show Gist options
  • Save davidbarredo/7480937 to your computer and use it in GitHub Desktop.
Save davidbarredo/7480937 to your computer and use it in GitHub Desktop.
Image data on change input file type
var url = window.URL || window.webkitURL;
// Change inputFileID with your own
document.getElementById("inputFileID").onchange = function() {
if ( this.disabled ) {
// File upload not allowed
console.log( 'Your browser does not support File upload.' );
} else {
var fileChosen = this.files[0];
var image = new Image();
image.onload = function() {
// Do something with the data
console.log( 'Width:'+this.width +' Height:'+ this.height+' '+ Math.round(fileChosen.size/1024)+'KB' );
};
image.onerror = function() {
// Error
console.log( 'Not a valid file type: '+ fileChosen.type );
};
image.src = url.createObjectURL(fileChosen);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment