Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应-检查用户选择图像的尺寸_Reactjs_Image_Validation_Image Uploading - Fatal编程技术网

Reactjs 反应-检查用户选择图像的尺寸

Reactjs 反应-检查用户选择图像的尺寸,reactjs,image,validation,image-uploading,Reactjs,Image,Validation,Image Uploading,我有一个文件选择器,它在将图像发送到服务器进行服务器端处理之前调用functin来验证图像。我正在尝试确保上传的图像是png或jpg并且在2mb下。我还试图确保它的尺寸不超过200乘200。我能够成功地检查文件类型和大小,但无法获得尺寸 这是我的密码: profilePicInputChange(event){ var pattern = /^[\w,\s-]+\.[A-Za-z]{3}$/; var filepath = event.target.value.split("\\")

我有一个文件选择器,它在将图像发送到服务器进行服务器端处理之前调用functin来验证图像。我正在尝试确保上传的图像是
png
jpg
并且在
2mb
下。我还试图确保它的尺寸不超过200乘200。我能够成功地检查文件类型和大小,但无法获得尺寸

这是我的密码:

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
    });
  }
}
profilePicInputChange(事件){
变量模式=/^[\w\s-]+\.[A-Za-z]{3}$/;
var filepath=event.target.value.split(“\\”);
var filename=filepath.pop();
if(pattern.test(文件名)){
var extensionList=filename.split(“.”);
var extension=extensionList.pop();
如果(扩展名=='png'| |扩展名=='jpg'){

if(event.target.files[0].size可能与@Mathieu重复,我已看到并尝试回答您上面链接的问题,但没有结果。