使用API文件在提交的表格上验证图像尺寸高达150X50px

validate on form submit for image dimension upto 150X50px with file API

本文关键字:图像 验证 150X50px 高达 表格 文件 API 提交 使用      更新时间:2023-09-26

如何使用jquery中的文件API在表单上验证图像维度高达150X50px的提交。?

请帮忙。。

您可以从https://developer.mozilla.org/en-US/docs/Web/API/FileReader阅读如何做到这一点。以下是本页的代码片段。

对图像使用onLoad事件处理程序来获取高度和宽度属性以验证它们。下面是一个Javascript图像加载事件绑定的示例

<!doctype html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Image preview example</title>
<script type="text/javascript">
oFReader = new FileReader(), rFilter = /^(?:image'/bmp|image'/cis'-cod|image'/gif|image'/ief|image'/jpeg|image'/jpeg|image'/jpeg|image'/pipeg|image'/png|image'/svg'+xml|image'/tiff|image'/x'-cmu'-raster|image'/x'-cmx|image'/x'-icon|image'/x'-portable'-anymap|image'/x'-portable'-bitmap|image'/x'-portable'-graymap|image'/x'-portable'-pixmap|image'/x'-rgb|image'/x'-xbitmap|image'/x'-xpixmap|image'/x'-xwindowdump)$/i;
oFReader.onload = function (oFREvent) {
  document.getElementById("uploadPreview").src = oFREvent.target.result;
};
function loadImageFile() {
  if (document.getElementById("uploadImage").files.length === 0) { return; }
  var oFile = document.getElementById("uploadImage").files[0];
  if (!rFilter.test(oFile.type)) { alert("You must select a valid image file!"); return; }
  oFReader.readAsDataURL(oFile);
}
</script>
</head>
<body onload="loadImageFile();">
  <form name="uploadForm">
    <table>
      <tbody>
        <tr>
          <td><img id="uploadPreview" style="width: 100px; height: 100px;" alt="Image preview" /></td>
          <td><input id="uploadImage" type="file" name="myPhoto" onchange="loadImageFile();" /></td>
        </tr>
      </tbody>
    </table>
    <p><input type="submit" value="Send" /></p>
  </form>
</body>
</html>