我已经创建了一个type=file
输入元素
<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">
我需要使用纯Javascript检查文件分辨率是否为aXb
。如何在verifyFileUpload(event)
函数中做到这一点?
答案 0 :(得分:4)
尝试以下方式
window.URL = window.URL || window.webkitURL;
function verifyFileUpload(e)
{
var file = document.getElementById("input-id");
if (file && file.files.length > 0)
{
var img = new Image();
img.src = window.URL.createObjectURL( file.files[0] );
img.onload = function()
{
var width = img.naturalWidth,
height = img.naturalHeight;
console.log ("Image Width: " + width);
console.log ("Image Height: " +height);
};
}
}
<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">