-1
我想我通過JavaScript上傳圖像的大小,但我不能如何通過javascript獲取上傳的圖片大小?
HTML
<form>
<input type="file" id="img" multiple>
</form>
JS
var imageData = function(files) {
var imgWidth = 0,
imgHeight = 0;
var reader = new FileReader();
reader.addEventListener('load',function(theFile) {
var img = new Image();
img.src = theFile.target.result;
img.addEventListener('load',function(){
return {
imgWidth :this.width,
imgHeight :this.height
};
});
});
reader.readAsDataURL(files);
};
function checkImg() {
var files = imgs.files;
for (var i = 0; i < files.length; i++){
console.log(imageData(files[i]).imgWidth);
}
}
var imgs = document.getElementById('img');
imgs.addEventListener('change',checkImg);
http://jsbin.com/tuqoguwudo/3/edit?html,js,console,output
jsbin控制檯顯示一個非常明確的錯誤消息,不是嗎? – CBroe