将文件上传选择限制为特定类型 [英] restrict file upload selection to specific types

查看:44
本文介绍了将文件上传选择限制为特定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何要通过 <input type="file"/> 元素来限制文件类型的选择?

Anyway to restrict the selection of file types via the <input type="file" /> element?

例如,如果我只想上传图像类型,我会将可能的选择限制为 (image/jpg,image/gif,image/png),并且选择对话框会使其他 mime 类型的文件变灰.

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.

附言我知道我可以在事后通过扫描 .type 属性使用 File API 执行此操作.我真的很想事先限制这个..我也知道我可以通过闪光灯来做到这一点,但我不想为此使用闪光灯.

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.

推荐答案

有一个用于此特定目的的 html 属性,称为 accept,但它几乎没有跨浏览器的支持.因为这里推荐服务器端验证.

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.

<input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" />

如果您无法访问后端,请查看基于 Flash 的解决方案,例如 SWFUpload.

If you don't have access to the backend have a look at a flash based solution like SWFUpload.

在此处查看更多信息:文件输入接受"属性 - 是吗有用吗?

这篇关于将文件上传选择限制为特定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆