将文件上传选择限制为特定类型
无论如何要通过 元素限制文件类型的选择?
例如,如果我只想上传图像类型,我会将可能的选择限制为(image/jpg、image/gif、image/png),并且选择对话框会将其他 mime 类型的文件显示为灰色。
ps 我知道我可以在事后使用 File API 通过扫描 .type 属性来完成此操作。我真的想事先限制这一点..我也知道我可以通过闪光灯来做到这一点,但我不想为此使用闪光灯。
Anyway to restrict the selection of file types via the <input type="file" />
element?
For instance, if I wanted only images types to be uploaded, I would restrict the possible selections to (image/jpg,image/gif,image/png), and the selection dialog would grey out files of other mime types.
p.s. I know that I can do this after the fact with the File API by scanning the .type attributes. I'm really trying to restrict this before hand.. I also know I can do this via flash, but I do NOT want to have use flash for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个用于此特定目的的 html 属性,称为
accept
,但跨浏览器的支持很少。因此,建议改为服务器端验证。如果您无法访问后端,请查看基于 Flash 的解决方案,例如 SWFUpload 。
在此处查看更多信息:文件输入“接受”属性 - 有用吗?< /a>
There is an html attribute for this specific purpose called
accept
but it has little support across browsers. Because of this server side validation is recommended instead.If you don't have access to the backend have a look at a flash based solution like SWFUpload.
See more on this here: File input 'accept' attribute - is it useful?
最好让用户选择任何文件,然后检查其类型 - 浏览器更好地支持这一点:
您还可以使用 file.size 属性检查文件大小。
It's better to let user select any file, and then check its type - this is better supported by the browsers:
You can also check for file size using file.size property.