文件上传控件(input type='file')对文件类型的过滤

    在文件上传控件(input type='file')中,可以通过accept属性实现对文件类型的过滤。

    一、相关代码

    下面给出对应的文件上传控件代码:

<input type="file" id="myFile" accept="image/jpeg,image/gif,application/msword"/><br/>

    上面的代码表示限制文件上传类型为.jpg/.gif/.doc。

    二、限制规则

    在文件上传控件的accept属性中,接受以下两种格式的限制符:

  • 文件类型,但不限制具体扩展名,如:image/*,就只要是图片就行。
  • 文件类型+具体的文件扩展类型,如:image/jpeg,使用的是MIME_TYPE类型
  • 多个MIME_TYPE类型之间用逗号分隔。

    三、accept属性具体接受的类型

    以下是几个常用的类型,其它的可以查询相关资料:

  • .doc               application/msword
  • .jpg                image/jpg
  • .txt                text/plain
  • .xls                application/vnd.ms-excel
原文地址:https://www.cnblogs.com/huatao/p/4736202.html