0

I have a file selector that calls a functin to validate the image before sending it to the server for server side processing. I am trying to make sure that the uploaded image is either a png or jpg and is under 2mb. I am also trying to make sure that its dimensions do not exceed 200 by 200. I am able to successfuylly check the file type and size but cannot get the dimensions.

Here is my code:

profilePicInputChange(event){

  var pattern = /^[\w,\s-]+\.[A-Za-z]{3}$/;

  var filepath = event.target.value.split("\\");
  var filename = filepath.pop();

  if(pattern.test(filename)){

    var extensionList = filename.split(".");
    var extension = extensionList.pop();

    if(extension === 'png' || extension === 'jpg'){
      if(event.target.files[0].size <= 2000000){
        document.getElementById("invalidLicence").innerHTML = "";
        this.setState({
          profilePic: event.target.files[0],
          profilePicValid: true
        });
      }else{
        document.getElementById("invalidProfilePic").innerHTML = "<strong>Please Upload a file less than 2MB.</strong>";
        document.getElementById("invalidProfilePic").style.color = "red";
        this.setState({
          profilePicValid: false
        });
      }
    }else{
      document.getElementById("invalidProfilePic").innerHTML = "<strong>Please Upload a valid file type.</strong>";
      document.getElementById("invalidProfilePic").style.color = "red";
      this.setState({
        profilePicValid: false
      });
    }
  }else{
    document.getElementById("invalidProfilePic").innerHTML = "<strong>Please Upload a file with a valid name.</strong>";
    document.getElementById("invalidProfilePic").style.color = "red";
    this.setState({
      profilePicValid: false
    });
  }
}

EDIT 1

Probably should have mentioned this originally, but I attempted the answers in this question but could not get the desired result:

Is it possible to check dimensions of image before uploading?

  • Possible duplicate of [Is it possible to check dimensions of image before uploading?](https://stackoverflow.com/questions/13572129/is-it-possible-to-check-dimensions-of-image-before-uploading) – Mathieu Oct 19 '19 at 23:05
  • @Mathieu I have seen and attempted the answers in the question you linked above to no avail. – Christopher Littlewood Oct 19 '19 at 23:19

0 Answers0