type
status
date
slug
summary
tags
category
icon
password
上次编辑时间
Apr 4, 2025 02:47 PM
当我们使用表单组件的时候,经常会使用到文件选择控件:
<input type="file" />
,有时候我们会忽略掉其一个重要的属性 accept
,其代表这个上传控件允许浏览的文件类型,使用这个属性可以大大改善用户选择文件时的体验。
accept 属性
根据 MDN 文档,其这样解释
accepet
属性:accept 属性是一个字符串,它定义了文件 input 应该接受的文件类型。这个字符串是一个以逗号为分隔的 唯一文件类型说明符 列表。由于给定的文件类型可以用多种方式指定,因此当你需要给定格式的文件时,提供一组完整的类型指定符是非常有用的。
那什么是唯一文件类型说明符呢?唯一文件类型说明符是一个字符串,表示在 file 类型的
<input>
元素中用户可以选择的文件类型。每个唯一文件类型说明符可以采用下列形式之一:- 一个以英文句号(".")开头的合法的不区分大小写的文件名扩展名。例如: .jpg,.pdf 或 .doc。
- 一个不带扩展名的 MIME 类型字符串。
- 字符串 audio/*, 表示“任何音频文件”。
- 字符串 video/*,表示 “任何视频文件”。
- 字符串 image/*,表示 “任何图片文件”。
accept 属性的值是一个包含一个或多个(用逗号分隔)这种唯一文件类型说明符的字符串。 例如,一个文件选择器需要能被表示成一张图片的内容,包括标准的图片格式和 PDF 文件,大概是这样的:
MIME
光只有音频、视频、图片大家可能觉得不够用,我们经常会提交表格或其他文档文件,这个时候就需要使用到上述提到的 MIME 类型字符串了。
MIME
通常称为 Multipurpose Internet Mail Extensions 或 MIME 类型 )是一种标准,用来表示文档、文件或字节流的性质和格式。其通常结构是这样:
type/subtype
,由类型与子类型两个字符串中间用'/'分隔而组成,type 表示可以被分多个子类的独立类别。subtype 表示细分后的每个类型。例如:
知道这些,选择文件的时候就好办了,只要知道对应的文件 MIME type 就好了,接下来我列出了基本上你能见到的 MIME type。
扩展名 | 文档类型 | MIME 类型 |
.aac | AAC audio | audio/aac |
.abw | AbiWord 文档 | application/x-abiword |
.arc | Archive 文档 | application/x-freearc |
.avi | AVI 视频 | video/x-msvideo |
.azw | Amazon Kindle 电子书 | application/vnd.amazon.ebook |
.bin | 二进制文件 | application/octet-stream |
.bmp | Windows OS/2 Bitmap Graphics | image/bmp |
.bz | BZip 压缩文档 | application/x-bzip |
.bz2 | BZip2 压缩文档 | application/x-bzip2 |
.csh | C-Shell script | application/x-csh |
.css | Cascading Style Sheets (CSS) | text/css |
.csv | Comma-separated values (CSV) | text/csv |
.doc | Microsoft Word | application/msword |
.docx | Microsoft Word (OpenXML) | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.eot | MS Embedded OpenType fonts | application/vnd.ms-fontobject |
.epub | Electronic publication (EPUB) | application/epub+zip |
.gif | Graphics Interchange Format (GIF) | image/gif |
.htm .html | HyperText Markup Language (HTML) | text/html |
.ico | Icon format | image/vnd.microsoft.icon |
.ics | iCalendar format | text/calendar |
.jar | Java Archive (JAR) | application/java-archive |
.jpeg.jpg | JPEG images | image/jpeg |
.js | JavaScript | text/javascript |
.json | JSON format | application/json |
.jsonld | JSON-LD format | application/ld+json |
.mid.midi | Musical Instrument Digital Interface (MIDI) | audio/midi audio/x-midi |
.mjs | JavaScript module | text/javascript |
.mp3 | MP3 audio | audio/mpeg |
.mpeg | MPEG Video | video/mpeg |
.mpkg | Apple Installer Package | application/vnd.apple.installer+xml |
.odp | OpenDocument presentation document | application/vnd.oasis.opendocument.presentation |
.ods | OpenDocument spreadsheet document | application/vnd.oasis.opendocument.spreadsheet |
.odt | OpenDocument text document | application/vnd.oasis.opendocument.text |
.oga | OGG audio | audio/ogg |
.ogv | OGG video | video/ogg |
.ogx | OGG | application/ogg |
.otf | OpenType font | font/otf |
.png | Portable Network Graphics | image/png |
.pdf | Adobe Portable Document Format (PDF) | application/pdf |
.ppt | Microsoft PowerPoint | application/vnd.ms-powerpoint |
.pptx | Microsoft PowerPoint (OpenXML) | application/vnd.openxmlformats-officedocument.presentationml.presentation |
.rar | RAR archive | application/x-rar-compressed |
.rtf | Rich Text Format (RTF) | application/rtf |
.sh | Bourne shell script | application/x-sh |
.svg | Scalable Vector Graphics (SVG) | image/svg+xml |
.swf | Small web format (SWF) or Adobe Flash document | application/x-shockwave-flash |
.tar | Tape 压缩文档 | application/x-tar |
.tif .tiff | Tagged Image File Format (TIFF) | image/tiff |
.ttf | TrueType Font | font/ttf |
.txt | Text, (generally ASCII or ISO 8859-n) | text/plain |
.vsd | Microsoft Visio | application/vnd.visio |
.wav | Waveform Audio Format | audio/wav |
.weba | WEBM audio | audio/webm |
.webm | WEBM video | video/webm |
.webp | WEBP image | image/webp |
.woff | Web Open Font Format (WOFF) | font/woff |
.woff2 | Web Open Font Format (WOFF) | font/woff2 |
.xhtml | XHTML | application/xhtml+xml |
.xls | Microsoft Excel | application/vnd.ms-excel |
.xlsx | Microsoft Excel (OpenXML) | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xml | XML | |
.xul | XUL | application/vnd.mozilla.xul+xml |
.zip | ZIP 压缩文档 | application/zip |
.3gp | audio/video container | video/3gppaudio/3gpp |
.3g2 | audio/video container | video/3gpp2audio/3gpp2 |
.7z | 7-zip 压缩文档 | application/x-7z-compressed |
提示
需要注意的是,仅标注了 accept 属性是不够的,这样只是提升了用户体验,但是对于文件的类型验证,这一招只防君子不防小人,用户可以轻松绕过类型选择限制。选择文件时点击右下角的类别选择
所有文件
,照样能选到其他类型。所以文件选中后是要使用正则表达式来验证其文件名合法性的。
如果你觉得上述的 MIME 不够用,你还可以使用开源项目
mime
这个npm库,基本上涵盖了所有文件类型,而且在不断更新。使用方法:
- 作者:Tsing
- 链接:https://www.huqing.site/front-end/1cb94e5f-a0ad-80c2-b49a-cd1ff9e7ae34
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。