Home > Web Front-end > JS Tutorial > How to determine file type size in js

How to determine file type size in js

亚连
Release: 2018-06-13 17:56:45
Original
2318 people have browsed it

Below I will share with you an implementation method of JS judging the file type size and giving prompts. It has a good reference value and I hope it will be helpful to everyone.

Uploading files is a commonly used function in work. Different scenarios have different requirements for different file types and file sizes:

<form id="uploadForm" method="post" class="layui-form">
  <p class="layui-form-item">
  <label class="layui-form-label">名称</label>
  <p class="layui-input-block">
   <input type="text" name="name" required lay-verify="required"
   placeholder="请输入文件名" autocomplete="off" class="layui-input">
  </p>
  </p>
  <p class="layui-form-item">
  <label class="layui-form-label">资料类型:</label>
  <p class="layui-input-block">
   <select name="datatypeid" id="datatypeid"></select>
  </p>
  <input type="hidden" id="yincang">
  </p>
  <p class="layui-form-item">
  <label class="layui-form-label">上传文件</label>
  <p class="layui-input-block">
   <input class="layui-input" type="file" name="file" onchange="fileChange(this);"/>
  </p>
  </p>
  <p class="layui-form-item">
  <p class="layui-input-block">
   <input type="button" class="layui-btn" value="上传"
   onclick="upload()" />
  </p>
  </p>
 </form>
Copy after login

js method:

<script type="text/javascript">
 var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
 function fileChange(target, id) {
  var fileSize = 0;
  var filetypes = [ ".doc", ".docx" ];//这里设置接受的文件类型
  var filepath = target.value;
  var filemaxsize = 1024 * 10;//接受的文件最大10M 
  if (filepath) {
  var isnext = false;
  var fileend = filepath.substring(filepath.indexOf("."));
  if (filetypes && filetypes.length > 0) {
   for (var i = 0; i < filetypes.length; i++) {
   if (filetypes[i] == fileend) {
    isnext = true;
    break;
   }
   }
  }
  if (!isnext) {
   alert("不接受此文件类型!");
   target.value = "";
   return false;
  }
  } else {
  return false;
  }
  if (isIE && !target.files) {
  var filePath = target.value;
  var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
  if (!fileSystem.FileExists(filePath)) {
   alert("附件不存在,请重新输入!");
   return false;
  }
  var file = fileSystem.GetFile(filePath);
  fileSize = file.Size;
  } else {
  fileSize = target.files[0].size;
  }
  var size = fileSize / 1024;
  if (size > filemaxsize) {
  alert("附件大小不能大于" + filemaxsize / 1024 + "M!");
  target.value = "";
  return false;
  }
  if (size <= 0) {
  alert("附件大小不能为0M!");
  target.value = "";
  return false;
  }
 }
 </script>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to determine NaN in JavaScript

How to use jQuery to implement mouse-responsive transparency gradient animation effect

How to implement mouse-responsive Taobao animation effect in jQuery

The above is the detailed content of How to determine file type size in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template