在网络浏览器中浏览具有特定扩展名的文件?
在我们基于网络的应用程序中,我们允许用户上传文件。
我想知道点击浏览按钮后我们是否可以限制可用文件列表通过文件扩展名上传。例如 *.zip
。只是为了让用户更轻松地查找、选择并单击文件名。
我想会有 javascript 基础解决方案,但是我可以在没有 javascript 的情况下做到这一点吗?
注意:此问题仅涉及浏览对话框以显示更少的文件。
In our web based application we allow users to upload files.
I wonder if after clicking browse button we can restrict the list of available files to be uploaded by file extension. *.zip
for example. Just to give the user easier way to find, select and click the file name.
I guess there will be javascript base solution but can I do it without javascript?
NOTE: this questions is only about making the browse dialogue to display less files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理论上,
应该可以工作。遗憾的是,没有主流浏览器支持它...
您可以使用 JavaScript 仅允许 zip 文件,但它们仍然会出现在列表中。
编辑:
出于限制目的,您可以使用 JavaScript,但仍然必须执行服务器端验证(例如使用 PHP)。
为了更方便的对话,您可以使用 Flash 或 Java Applet 解决方案。
链接:
Flash 上传器
JUpload
两者都允许您限制文件扩展名(以及许多其他自定义项)。
In theory,
<input type="file" accept="application/zip">
should work. Sadly, no mayor browser supports it...You can use JavaScript to allow zip files only, but they will still appear in the list.
Edit:
For restriction purposes, you could use JavaScript, but you'd still have to perform server-side verification (with PHP, for example).
For a more convenient dialogue, you could use a Flash or Java Applet solution.
Links:
Flash Uploader
JUpload
Both allow you to limit the file extensions (among many other customizations).
无法为文件浏览对话框设置文件扩展名过滤器。客户端最好的做法是在提交时通过 JavaScript 函数传递文件名,并在继续上传之前解析扩展名以查看其是否合法。
请始终记住,无论您尝试在客户端执行什么操作,您的用户仍然可以上传他们选择的任何文件。您应该始终进行服务器端检查,以确保人们没有上传恶意脚本。
It is not possible to set a file extension filter for the file browse dialogue. The best you can do client-side is to pass the file name through a javascript function on submit, and parse the extension to see if it is legal before proceeding with the upload.
Always bare in mind that your users will still be able to upload any files they choose, no-matter what you try to do client-side. You should always have a server-side check to ensure people aren't uploading malicious scripts.