【问题标题】:Do the values in the accept attribute require a computer to know the mime types?接受属性中的值是否需要计算机知道 MIME 类型?
【发布时间】:2013-02-06 07:24:00
【问题描述】:

所以我有一个标准的<input type="file" accept="..."/>,其中接受的是不同mime 类型的大列表。 我已经在服务器后端验证了文件类型,但我想确保我没有过滤掉客户端 UI 上的有效文件。我意识到旧的浏览器根本不会过滤文件,这很好。我担心的是现代浏览器可能无法识别 mime 类型并过滤掉实际有效的文件。

所以我的问题基本上是,“接受”属性有多可靠?例如,如果未安装 microsoft office,浏览器会识别 .docx mime 类型吗?此外,某些文件类型有许多不同的可能的 mime 类型与之关联,如果我只列出其中一种 mime 类型是否重要?

我不明白为什么规范最初使用 mime 类型,它会简单得多,而且我猜如果他们刚开始使用扩展名(如 chrome 接受)会更可靠。

关于我目前使用的 mime 的完整列表:

application/msword,
application/vnd.openxmlformats-officedocument.wordprocessingml.document,
application/rtf,
application/msexcel,
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
application/vnd.openxmlformats-officedocument.spreadsheetml.template,
application/vnd.ms-excel.template.macroEnabled.12,
application/vnd.ms-excel.sheet.macroEnabled.12,
text/csv,
application/mspowerpoint,
application/vnd.openxmlformats-officedocument.presentationml.presentation,
application/vnd.ms-powerpoint.presentation.macroEnabled.12,
application/vnd.ms-powerpoint,
application/vnd.openxmlformats-officedocument.presentationml.template,
application/vnd.openxmlformats-officedocument.presentationml.slideshow,
application/pdf,
image/jpg,
image/jpeg,
image/png,
image/bmp,
image/gif,
text/plain,
message/rfc822,
application/vnd.oasis.opendocument.text,
application/vnd.oasis.opendocument.spreadsheet,
application/vnd.oasis.opendocument.presentation,
application/visio,application/vnd.visio,
application/vst,
application/vnd.visio.webdrawing,
application/x-mspublisher

【问题讨论】:

    标签: html browser mime-types html-input


    【解决方案1】:

    The specification allows you to use extensions as well as mime types

    实际上,它实际上建议您同时使用 mime-type 和extension。

    至于要求 OS 识别 mime-type,这实际上没有意义,因为 mime-types 是 Internet 协议文件标识符。它实际上取决于用户代理(浏览器)对 mime 类型的支持。由于这是一个相当新的结构,如果浏览器只是通过在 mime 类型和扩展之间建立一个公共映射来最低限度地“支持”它,我不会感到惊讶。我无法想象浏览器会尝试统计所有可读文件以确定 mime 类型(根据我的经验,这甚至不接近可靠)。 accept 的意图似乎更多是为了改变文件上传对话框的外观,我怀疑任何浏览器都这样做了,尽管考虑到现在浏览器的发布速度有多快,我肯定是错的。

    如果您仍然担心,您可以在常见的浏览器中测试自己,因为它看起来应该是每个用户代理而不是每个操作系统。

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2015-05-26
      • 2015-11-25
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      相关资源
      最近更新 更多