How to determine file type size in js
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>
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What should I do if Google Chrome prompts that the content of this tab is being shared? When we use Google Chrome to open a new tab, we sometimes encounter a prompt that the content of this tab is being shared. So what is going on? Let this site provide users with a detailed introduction to the problem of Google Chrome prompting that the content of this tab is being shared. Google Chrome prompts that the content of this tab is being shared. Solution: 1. Open Google Chrome. You can see three dots in the upper right corner of the browser "Customize and control Google Chrome". Click the icon with the mouse to change the icon. 2. After clicking, the menu window of Google Chrome will pop up below, and the mouse will move to "More Tools"

How to open a received file in .et format? When I send files to friends and colleagues, I receive files with the .et suffix. Cannot be opened using default program. It turns out that .et is the default table file format saved by WPS and cannot be opened by Microsoft Excel. How can I open this document without installing WPS? After the phone receives the file, you can select "Open with other applications" and then choose to upload to the mailbox. Taking QQ mailbox as an example, you can upload by sending an email to yourself. If the file is received on the computer, just open the mailbox and upload it. After uploading to the mailbox, click the attachment preview to preview the file in .et file format. This avoids having to install software just to open a file. Of course for

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3
![How to increase disk size in VirtualBox [Guide]](https://img.php.cn/upload/article/000/887/227/171064142025068.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
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

Baidu Tieba app prompts that the operation is too frequent. This prompt is usually to maintain the normal operation and user experience of the platform to prevent malicious screen spam, advertising spam and other inappropriate behaviors. For specific handling methods, you can read the tutorial shared by the editor. Baidu Tieba app prompts that the operation is too frequent. Sharing how to deal with it 1. When the system prompts [Operation is too frequent], we need to wait for a while. If you are anxious, you can do something else first. Generally, after waiting for a while, this prompt message will It will disappear automatically and we can use it normally. 2. If after waiting for a long time, it still displays [Operation Too Frequent], we can try to go to Tieba Emergency Bar, Tieba Feedback Bar and other official Tieba, post to report this phenomenon and ask official personnel to solve it. 3.

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
