jquery 如何在点击“选择推荐分辨率的文件”按钮时应用图像分辨率验证

eiee3dmh  于 2023-03-07  发布在  jQuery
关注(0)|答案(1)|浏览(202)

我有〈输入类型=文件和一个按钮,以更新该文件的服务器。我想在其中一个按钮,它将验证上传的文件分辨率(宽度和高度)点击功能

0g0grzrc

0g0grzrc1#

function validateImageFile(fileInput) {
  const file = fileInput.files[0];
  const reader = new FileReader();

  reader.onload = function(event) {
    const img = new Image();
    img.onload = function() {
      const width = this.width;
      const height = this.height;

      if (width === 0 || height === 0) {
        alert("Invalid image file. Please select a valid image.");
        fileInput.value = "";
        // added a condtion , you chnage it according to your need
      } else if (width < 600 || height < 400) {
        alert("Image resolution is low. Please select an image with at least 600 x 400 resolution.");
        fileInput.value = "";
      } else {
        // Image is valid.
      }
    };
    img.src = event.target.result;
  };
  reader.readAsDataURL(file);
}

相关问题