最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Input type file check image resolution using pure Javascript - Stack Overflow

programmeradmin0浏览0评论

I have created a type=file input element

<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">

I need to check that the file resolution is aXb using pure Javascript. How can I do that in the verifyFileUpload(event) function?

I have created a type=file input element

<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">

I need to check that the file resolution is aXb using pure Javascript. How can I do that in the verifyFileUpload(event) function?

Share Improve this question edited Oct 3, 2018 at 12:30 Hary 5,8388 gold badges48 silver badges91 bronze badges asked Oct 3, 2018 at 12:09 S_SS_S 1,4125 gold badges28 silver badges58 bronze badges 3
  • event.target.files return selected files data – Mohammad Commented Oct 3, 2018 at 12:15
  • 2 Possible duplicate of stackoverflow./questions/8903854/… – clabe45 Commented Oct 3, 2018 at 12:25
  • This questions has lot of answers online, even on stack overflow, please consider doing a quick search before posting a question. also Possible duplicate stackoverflow./questions/12570834/… – Wasim Sayyed Commented Oct 3, 2018 at 12:32
Add a ment  | 

1 Answer 1

Reset to default 10

Try the below way

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);
        };
    }
}
<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">

发布评论

评论列表(0)

  1. 暂无评论