accept属性中的值是否需要计算机才能知道mime类型?

时间:2013-02-21 04:01:59

标签: html html5 browser mime-types html-input

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

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

我不明白为什么规范最初使用的是mime-types,它会显着简单,如果他们刚刚使用了扩展(比如chrome接受),我猜测它更可靠。

有关我目前正在使用的哑剧的完整列表:

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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