HTML input file control limits the type of uploaded files

HTML input file control limits the type of uploaded files

Add an input file HTML control to the web page:

<input id="File1" type="file" />

By default, all file types will be displayed. What if you want to limit it to only display the file types you set, such as "word", "excel", and "pdf" files?

The solution is to add an accept attribute to it, for example:

<input id="File1" type="file" accept=".xls,.doc,.txt,.pdf" />

When this is selected, the default display will be like this:

The file selection box only displays files of your custom file type, which is quite convenient.

However, this is just the simplest way to cover up the problem. You can still choose other file types:

Therefore, if you want to truly limit the type of practice (in fact, this is not a restriction, it just displays the file type you want by default, which does not mean that you cannot choose other ones), you still have to control it through js or the background.

Attached are the supported file types:

*.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video
*.ac3 audio/ac3 AC3 Audio
*.asf allpication/vnd.ms-asf Advanced Streaming Format
*.au audio/basic AU Audio
*.css text/css Cascading Style Sheets
*.csv text/csv Comma Separated Values
*.doc application/msword MS Word Document
*.dot application/msword MS Word Template
*.dtd application/xml-dtd Document Type Definition
*.dwg image/vnd.dwg AutoCAD Drawing Database
*.dxf image/vnd.dxf AutoCAD Drawing Interchange Format
*.gif image/gif Graphic Interchange Format
*.htm text/html HyperText Markup Language
*.html text/html HyperText Markup Language
*.jp2 image/jp2 JPEG-2000
*.jpe image/jpeg JPEG
*.jpeg image/jpeg JPEG
*.jpg image/jpeg JPEG
*.js text/javascript, application/javascript JavaScript
*.json application/json JavaScript Object Notation
*.mp2 audio/mpeg, video/mpeg MPEG Audio/Video Stream, Layer II
*.mp3 audio/mpeg MPEG Audio Stream, Layer III
*.mp4 audio/mp4, video/mp4 MPEG-4 Audio/Video
*.mpeg video/mpeg MPEG Video Stream, Layer II
*.mpg video/mpeg MPEG Video Stream, Layer II
*.mpp application/vnd.ms-project MS Project Project
*.ogg application/ogg, audio/ogg Ogg Vorbis
*.pdf application/pdf Portable Document Format
*.png image/png Portable Network Graphics
*.pot application/vnd.ms-powerpoint MS PowerPoint Template
*.pps application/vnd.ms-powerpoint MS PowerPoint Slideshow
*.ppt application/vnd.ms-powerpoint MS PowerPoint Presentation
*.rtf application/rtf, text/rtf Rich Text Format
*.svf image/vnd.svf Simple Vector Format
*.tif image/tiff Tagged Image Format File
*.tiff image/tiff Tagged Image Format File
*.txt text/plain Plain Text
*.wdb application/vnd.ms-works MS Works Database
*.wps application/vnd.ms-works Works Text Document
*.xhtml application/xhtml+xml Extensible HyperText Markup Language
*.xlc application/vnd.ms-excel MS Excel Chart
*.xlm application/vnd.ms-excel MS Excel Macro
*.xls application/vnd.ms-excel MS Excel Spreadsheet
*.xlt application/vnd.ms-excel MS Excel Template
*.xlw application/vnd.ms-excel MS Excel Workspace
*.xml text/xml, application/xml Extensible Markup Language
*.zip aplication/zip Compressed Archive

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  How to generate PDF and download it in Vue front-end

>>:  A brief analysis of MySQL's lru linked list

Recommend

Detailed explanation of generic cases in TypeScript

Definition of Generics // Requirement 1: Generics...

How to use and limit props in react

The props of the component (props is an object) F...

How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation fail...

Quickly get started with VUE 3 teleport components and usage syntax

Table of contents 1. Introduction to teleport 1.1...

Sharing some details about MySQL indexes

A few days ago, a colleague asked me a question a...

How to choose between MySQL CHAR and VARCHAR

Table of contents VARCHAR and CHAR Types Conclusi...

Implementation of mysql8.0.11 data directory migration

The default storage directory of mysql is /var/li...

mysql calculation function details

Table of contents 2. Field concatenation 2. Give ...

Detailed explanation of HTML style tags and related CSS references

HTML style tag style tag - Use this tag when decl...

Tips for implementing list loop scrolling based on jQuery (super simple)

I saw a good idea and recorded it. I have used jQ...

Django+mysql configuration and simple operation database example code

Step 1: Download the mysql driver cmd enters the ...

Full steps to create a password generator using Node.js

Table of contents 1. Preparation 2. Writing comma...

Implementation of remote Linux development using vscode

Say goodbye to the past Before vscode had remote ...