3

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?

Hary
  • 5,690
  • 7
  • 42
  • 79
S_S
  • 1,276
  • 4
  • 24
  • 47
  • `event.target.files` return selected files data – Mohammad Oct 03 '18 at 12:15
  • 2
    Possible duplicate of https://stackoverflow.com/questions/8903854/check-image-width-and-height-before-upload-with-javascript – clabe45 Oct 03 '18 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 https://stackoverflow.com/questions/12570834/how-to-preview-image-get-file-size-image-height-and-width-before-upload – Wasim Sayyed Oct 03 '18 at 12:32

1 Answers1

8

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)">
Juan
  • 15
  • 5
Hary
  • 5,690
  • 7
  • 42
  • 79