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?

3
  • event.target.files return selected files data Commented Oct 3, 2018 at 12:15
  • 2
    Possible duplicate of stackoverflow.com/questions/8903854/… 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.com/questions/12570834/… Commented Oct 3, 2018 at 12:32

1 Answer 1

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)">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.