信息转自 https://segmentfault.com/a/1190000017281291

Chrome获取MIME类型

在chromium开源代码中
https://cs.chromium.org/chromium/src/net/base/mime_util.cc?l=314 314-318行中提到了:

1
2
3
4
5
// We implement the same algorithm as Mozilla for mapping a file extension to
// a mime type. That is, we first check a hard-coded list (that cannot be
// overridden), and then if not found there, we defer to the system registry.
// Finally, we scan a secondary hard-coded list to catch types that we can
// deduce but that we also want to allow the OS to override.

Chrome实现了与Mozilla相同的算法,将文件扩展名映射到MIME类型。
首先,Chrome会检测一个硬编码列表(不能被覆盖)源码中的kPrimaryMappings,然后如果没有找到符合的,Chrome会从操作系统注册表中找,最后会扫描一个二级硬编码列表,源码中的kSecondaryMappings,用来捕获可以推断但是也希望允许操作系统覆盖的类型。

例如:从安装了Microsoft Excel的Windows系统上传CSV文件时,Chrome会将其报告为application/vnd.ms-excel。这是因为.csv未在第一个硬编码列表中指定,因此浏览器会回退到系统注册表。HKEY_CLASSES_ROOT.csv有一个名为的值Content Type设置为application/vnd.ms-excel。

几台windows 和 mac 机器测试,有一台win10,装有office的情况下,上传ppt 文件无法获取 文件 type (MIME)。根据上文里面资料推断,这台及其的office 注册表可能有问题,导致上传时候从机器找不到这个类型的MIME。

一个解决办法是,对于第三方安装的应用的类型,前端还是只做文件名后缀名的判断…shit!