Home Web Front-end JS Tutorial js method to determine file type size and give prompts

js method to determine file type size and give prompts

Jan 05, 2018 am 09:27 AM
javascript size

This article mainly shares 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. Let’s follow the editor to take a look, I hope it can help 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

Related Recommended:

Python accurately determines the file type

php code sharing to determine the file type based on the file header

PHP implementation code for reading file headers to determine file type_PHP tutorial

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to increase disk size in VirtualBox [Guide] How to increase disk size in VirtualBox [Guide] Mar 17, 2024 am 10:10 AM

We often encounter situations where the predefined disk size has no room for more data? If you need more virtual machine hard disk space at a later stage, you must expand the virtual hard disk and partitions. In this post, we will see how to increase disk size in VirtualBox. Increasing the disk size in VirtualBox It is important to note that you may want to back up your virtual hard disk files before performing these operations, as there is always the possibility of something going wrong. It is always a good practice to have backups. However, the process usually works fine, just make sure to shut down your machine before continuing. There are two ways to increase disk size in VirtualBox. Expand VirtualBox disk size using GUI using CL

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles