我想限制可以从本机操作系统文件选择器中选择的文件类型,当用户单击HTML中的<input type="file">元素中的Browse按钮时。我感觉这是不可能的,但我想知道是否有解决办法。我希望只使用HTML和JavaScript;请不要用闪光灯。
当前回答
我的建议如下:
If you have to make user select any of image files by default, the use accept="image/*" <input type="file" accept="image/*" /> if you want to restrict to specific image types then use accept="image/bmp, image/jpeg, image/png" <input type="file" accept="image/bmp, image/jpeg, image/png" /> if you want to restrict to specific types then use accept=".bmp, .doc, .pdf" <input type="file" accept=".bmp, .doc, .pdf" /> You cannot restrict user to change file filer to all files, so always validate file type in script and server
其他回答
正如在前面的回答中提到的,我们不能限制用户仅为给定的文件格式选择文件。但是在html中的file属性上使用accept标记非常方便。
至于验证,我们必须在服务器端进行。我们也可以在js的客户端这样做,但这不是一个万无一失的解决方案。我们必须在服务器端进行验证。
对于这些需求,我真的更喜欢struts2 Java web应用程序开发框架。通过其内置的文件上传功能,将文件上传到基于struts2的web应用程序中是小菜一碟。只要提到我们希望在应用程序中接受的文件格式,其余的都由框架本身的核心处理。您可以在struts的官方网站上查看它。
使用带有accept属性的输入标记
<input type="file" name="my-image" id="image" accept="image/gif, image/jpeg, image/png" />
点击这里查看最新的浏览器兼容性表
现场演示在这里
如果只选择图像文件,可以使用accept="image/*"
<input type="file" name="my-image" id="image" accept="image/*" />
现场演示在这里
只有gif, jpg和png将显示,屏幕截图从Chrome 44版
我知道有点晚了。
function Validatebodypanelbumper(theForm)
{
var regexp;
var extension = theForm.FileUpload.value.substr(theForm.FileUpload1.value.lastIndexOf('.'));
if ((extension.toLowerCase() != ".gif") &&
(extension.toLowerCase() != ".jpg") &&
(extension != ""))
{
alert("The \"FileUpload\" field contains an unapproved filename.");
theForm.FileUpload1.focus();
return false;
}
return true;
}
输入标记有accept属性。然而,它在任何方面都不可靠。 浏览器很可能将其视为“建议”,这意味着用户将根据文件管理器的不同,有一个只显示所需类型的预选。他们仍然可以选择“所有文件”,上传任何他们想要的文件。
例如:
<form> <输入类型=“文件” 名称=“图片” id=“图片” 接受=“图像/gif, 图像/jpeg” /> </form>
在HTML5规范中阅读更多
请记住,它只是用于“帮助”用户找到正确的文件。 每个用户都可以向您的服务器发送任何请求。 您总是需要验证服务器端的所有内容。
所以答案是:不,你不能限制,但你可以设置一个预选,但你不能依赖它。
Alternatively or additionally you can do something similar by checking the filename (value of the input field) with JavaScript, but this is nonsense because it provides no protection and also does not ease the selection for the user. It only potentially tricks a webmaster into thinking he/she is protected and opens a security hole. It can be a pain in the ass for users that have alternative file extensions (for example jpeg instead of jpg), uppercase, or no file extensions whatsoever (as is common on Linux systems).
我的建议如下:
If you have to make user select any of image files by default, the use accept="image/*" <input type="file" accept="image/*" /> if you want to restrict to specific image types then use accept="image/bmp, image/jpeg, image/png" <input type="file" accept="image/bmp, image/jpeg, image/png" /> if you want to restrict to specific types then use accept=".bmp, .doc, .pdf" <input type="file" accept=".bmp, .doc, .pdf" /> You cannot restrict user to change file filer to all files, so always validate file type in script and server
推荐文章
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 我如何在网站上使用谷歌的Roboto字体?
- 如何禁用文本区域大小调整?
- 如何在HTML5中改变视频的播放速度?
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- 嵌套行与自举网格系统?
- 如何用JavaScript截屏一个div ?
- "全屏" <iframe> .
- 如何检测“搜索”HTML5输入的清除?
- Python列表目录,子目录和文件
- 我如何转义一个单引号?
- 提取正则表达式匹配的一部分
- time_t最终的类型定义是什么?
- 只有css的砌体布局
- 实时http流到HTML5视频客户端的最佳方法