Request uploaded image size with FILE API
select image file to upload, click on "Load" to see the message.
HTML
<h3>
Upload an image file.
</h3>
<h5>
(You will get a message if your image dimentions are larger than 200x200)
</h5>
<form action='#' onsubmit="return false;">
<input type='file' id='imgfile' />
<input type='button' id='btnLoad' value='Load' />
</form>
CSS
body {
font-family: sans-serif;
}
h5 {
font-weight: normal;
}
JavaScript
$('#btnLoad').on('click', function () {
loadImage();
});
function loadImage() {
var input, file, fr, img;
if (typeof window.FileReader !== 'function') {
write("The file API isn't supported on this browser yet.");
return;
}
input = document.getElementById('imgfile');
if (!input) {
write("Um, couldn't find the imgfile element.");
} else if (!input.files) {
write("This browser doesn't seem to support the `files` property of file inputs.");
} else if (!input.files[0]) {
write("Please select a file before clicking 'Load'");
} else {
file = input.files[0];
fr = new FileReader();
fr.onload = createImage;
fr.readAsDataURL(file);
}
function createImage() {
img = document.createElement('img');
img.onload = imageLoaded;
img.style.display = 'none'; // If you don't want it showing
img.src = fr.result;
document.body.appendChild(img);
}
function imageLoaded() {
write("Your image dimentions are: " + img.width + "x" + img.height);
if(img.width != 200){ write("width must be 200px")}
if(img.height != 200){write("height must be 200px")}
// This next bit removes the image, which is obviously optional -- perhaps you want
// to do something with it!
img.parentNode.removeChild(img);
img = undefined;
}
function write(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
}
}