如何制作<input type="file"/>只接受这些类型? [英] How to make <input type="file"/> accept only these types?
问题描述
我希望我的上传器只允许这些类型:
I want my uploader only allows these types:
- 文档,文档.
- xls,xlsx.
- ppt、pptx.
- txt.
- pdf.
- 图像类型.
我怎样才能做到这一点?我应该在 accept
属性中放什么?感谢您的帮助.
How can I achieve this? What should I put in the accept
attribute? Thanks for your help.
编辑!!!
我还有一件事要问.当出现用于选择文件的弹出窗口时,在右下角,有一个包含所有允许文件的下拉列表.就我而言,列表会很长.我在列表中看到,有一个名为 All Supported Types
的选项.如何使其默认选中并消除所有其他选项?
I have one more thing to ask. When the popup appears for use to choose file, at the down right corner, there is a drop down list contains all allow files. In my case, the list would be long. I see in the list, there is an option called All Supported Types
. How can I make it chosen by default and eliminate all other options?
任何帮助将不胜感激.谢谢.
Any help will be appreciated. Thank you.
推荐答案
accept
属性,根据 HTML5 LC,是一个 逗号分隔的项目列表,每个项目都是特定的媒体类型,如 image/gif
,或一个符号像 image/*
表示所有 image
类型,或者像 .gif
这样的文件扩展名.IE 10+ 和 Chrome 支持所有这些,而 Firefox 不支持这些扩展.因此,最安全的方法是使用 媒体类型 和诸如image/*
,在这种情况下
The value of the accept
attribute is, as per HTML5 LC, a comma-separated list of items, each of which is a specific media type like image/gif
, or a notation like image/*
that refers to all image
types, or a filename extension like .gif
. IE 10+ and Chrome support all of these, whereas Firefox does not support the extensions. Thus, the safest way is to use media types and notations like image/*
, in this case
<input type="file" name="foo" accept=
"application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint,
text/plain, application/pdf, image/*">
如果我正确理解意图.请注意,浏览器可能无法准确识别权威注册表中指定的媒体类型名称,因此需要进行一些测试.
if I understand the intents correctly. Beware that browsers might not recognize the media type names exactly as specified in the authoritative registry, so some testing is needed.
这篇关于如何制作<input type="file"/>只接受这些类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!