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?