HTML使用input上传文件类型限制

在上传文件的时候,我们经常会有特定的文件需求,比如只要word,或是excel,jpeg等等,这个是可以在前端的input来直接进行限制的.

在stackoverflow上找到的答案HTML Input=“file” Accept Attribute File Type (CSV).
对各种文件的input限制详细描述:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Valid Accept Types:
For CSV files (.csv), use:
<input type="file" accept=".csv" />
For Excel Files 97-2003 (.xls), use:
<input type="file" accept="application/vnd.ms-excel" />
For Excel Files 2007+ (.xlsx), use:
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
For Text Files (.txt) use:
<input type="file" accept="text/plain" />
For Image Files (.png/.jpg/etc), use:
<input type="file" accept="image/*" />
For HTML Files (.htm,.html), use:
<input type="file" accept="text/html" />
For Video Files (.avi, .mpg, .mpeg, .mp4), use:
<input type="file" accept="video/*" />
For Audio Files (.mp3, .wav, etc), use:
<input type="file" accept="audio/*" />
For PDF Files, use:
<input type="file" accept=".pdf" />
NOTE:
If you are trying to display Excel CSV files (.csv), do NOT use:
text/csv
application/csv
text/comma-separated-values (works in Opera only).
If you are trying to display a particular file type (for example, a WAV or PDF), then this will almost always work...
<input type="file" accept=".FILETYPE" />

文章目录
,