在前端html中,上传文件时,一般都是用到 type 属性值为 file 的 input 标签,但在默认情况下,file 类型的 input 标签是不限制选择本地文件的类型的,那么本篇博文,飞鸟慕鱼就来和大家说一说,input file 如何限制本地文件的类型。

input file 代码

<input type="file" name="Files" />

默认情况下,input file标签 选择本地文件时,是不受限制的,可以选择压缩包类型文件,文本类型文件,图片类型文件等。

input file限制上传文件类型的方法

input accept属性介绍

accept:此属性只能与 <input type="file"> 配合使用。它规定能够上传的文件类型

语法:

<input type="file" accept="value">

参数:

value:可选,具体参考文未的值

input file 只允许图片类型上传

1、只允许图片类型文件上传

设置accept 属性的值为 "image/*" ,只允许图片类型的文件上传

<!--只允许图片类型文件-->
<input type="file" name="Files" accept="image/*" />

input file 选择文件如下:

input file限制上传文件类型的方法


2、只允许单一类型的图片文件上传

html代码:

<!--只允许jpg类型的图片文件-->
<input type="file" name="Files" accept="image/jpeg" />

input file 选择文件如下:

input file限制上传文件类型的方法

input file 其它示例

html代码

<!--只允许文本文件-->
<input type="file" name="Files" accept="text/plain" />
<!--只允许png类型的图片文件-->
<input type="file" name="Files" accept="image/png" />
<!--只允许zip类型的压缩包文件-->
<input type="file" name="Files" accept="aplication/zip" />
<!--只允许PPT类型的文件-->
<input type="file" name="Files" accept="application/vnd.ms-powerpoint" />

input file accept属性部份值介绍

文件类型 accept 属性值
*.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

PS:

1、input 上传标签的 accept 属性,不能作为限制文件上传的唯一限制。

2、在网站后端接收前台上传的文件时,一定要注意检查