在 HTML 文件打开对话框中过滤文件
我正在使用 HTML 标签将文件上传到服务器:
<input type="file">
当我单击浏览按钮时,它会显示一个文件打开对话框。我可以通过将扩展过滤器传递到该对话框来过滤文件吗?就像我们可以在 .Net 框架的文件打开对话框中通过传递以下内容一样:
Text files *.txt|.txt
使用此过滤器我们只能打开 .txt
文件。其他不向用户显示的文件。这个对话框有选项吗?
I am working with file upload to the server using HTML tag:
<input type="file">
When I click on the browse button it shows me a file open dialog. Can I filter the files by passing the extension filter to that dialog? Like we can do in .Net framework's file open dialog by passing some thing like:
Text files *.txt|.txt
Using this filter we can only open .txt
files. Other files not shown to the user. Is there any option for this dialog?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
标准
实际上在 HTML5 中您可以设置
accept
属性,所以现在这是可能的! W3C 标准 规定:接受的值 [维基百科中的完整列表]
只需传递一个有效的 MIME 类型 到属性,例如:
audio/*
视频/*
视频/ogg
图像/*
图片/jpg
图像/bmp
示例代码
Standard
Actually in HTML5 you can set the
accept
attribute so now this is possible! The W3C standard states:Accepted values [Full list in Wikipedia]
Just pass a valid MIME type to the attribute for example:
audio/*
video/*
video/ogg
image/*
image/jpg
image/bmp
Example code
不,您不能使用普通的
type="file"
输入来执行此操作。您可以使用一些 Flash 上传控件来实现此目的。No you can't do this with the plain
type="file"
input. You could use some Flash upload controls though that allow you to achieve this.我认为您无法编辑此对话框选项,但您可以在用户选择文件后验证该文件。
要编辑对话框,我记得您可以通过 Flash 或 Silverlight 上传器(例如 swfUpload)来完成此操作。
I don't think you can edit this dialog options, but you can validate the file after the user select it.
tO edit the dialog, I remember that you can do that by a flash or Silverlight uploaders, such as swfUpload.
这是我在需要过滤某些文件类型时想到的一段代码:
Accept 属性支持多个文件以逗号分隔。
Here is a piece of code that I came up with when I was in need of filtering some of the file types:
Accept attribute supports multiple files comma separated.