使用纯 Javascript 输入类型文件检查图像分辨率

S_S*_*S_S 1 javascript image filereader

我创建了一个type=file输入元素

<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">
Run Code Online (Sandbox Code Playgroud)

我需要检查文件分辨率是否aXb使用纯 Javascript。我怎样才能在verifyFileUpload(event)函数中做到这一点?

mbh*_*n88 7

试试下面的方法

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 = this.naturalWidth,
                height = this.naturalHeight;
                
          console.log ("Image Width: " + width);
          console.log ("Image Height: " +height);
        };
    }
}
Run Code Online (Sandbox Code Playgroud)
<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">
Run Code Online (Sandbox Code Playgroud)