HTML 文件输入仅接受 .zip 文件(不接受 .exe)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24832275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:16:50  来源:igfitidea点击:

HTML file input accept only .zip files (not .exe)

htmlfilegoogle-chromefirefox

提问by 2778

using this for the accept field in my file input:

将此用于我的文件输入中的接受字段:

application/zip,application/x-zip,application/x-zip-compressed,application/octet-stream

Result:

结果:

  • Internet Explorer: Only shows .zip. Yay.
  • FireFox: shows all files, but I can change dropdown to only show .zip. Can I change .zip to be the default view somehow?
  • Chrome: shows .zip and .exe. How do I exclude .exe?
  • Internet Explorer:仅显示 .zip。好极了。
  • FireFox:显示所有文件,但我可以将下拉列表更改为仅显示 .zip。我可以以某种方式将 .zip 更改为默认视图吗?
  • Chrome:显示 .zip 和 .exe。如何排除 .exe?

I know chrome is technically right, but I'm trying to be user friendly here.

我知道 chrome 在技术上是正确的,但我试图在这里对用户友好。

采纳答案by Jason

application/octet-streamis the "catch-all" bucket. It's the default mime-type if the filename has no extension to help determine it. It's also the source of your EXE representation in Chrome, I'm guessing.

application/octet-stream是“包罗万象”的桶。如果文件名没有扩展名来帮助确定它,则它是默认的 MIME 类型。我猜它也是 Chrome 中 EXE 表示的来源。

Limit file format when using <input type="file">?

使用 <input type="file"> 时限制文件格式?

You can also try reducing it with the "accept" attribute on the INPUT/File. That link above also has a link to a JSFiddle page you can play and test with.

您还可以尝试使用 INPUT/File 上的“accept”属性来减少它。上面的链接还有一个指向 JSFiddle 页面的链接,您可以使用它进行播放和测试。

回答by Waruna Manjula

try

尝试

<!-- (IE 10+, Edge, Chrome, Firefox 42+) -->
<input type="file" accept=".zip,.rar,.7zip" />

<input type="file" accept="zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed">