我有一个web应用程序,用户需要上传一个.zip文件。在服务器端,我正在检查上传文件的MIME类型,以确保它是application/x-zip-compressed
或application/zip
。
在Firefox和IE上,这对我来说很好。然而,当一个同事在Firefox上测试它时,它失败了。
所以我的问题是:浏览器如何确定要发送的MIME类型?
发布于 2018-05-11 19:31:24
Chrome有三种方法可以确定MIME类型,并按特定顺序进行。下面的代码片段来自文件src/net/base/mime_util.cc
,方法MimeUtil::GetMimeTypeFromExtensionHelper
。
// 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.
再次使用相同的示例,浏览器将报告application/vnd.ms-excel
。我认为这是合理的假设Internet Explorer 使用注册表。它也可能使用Chrome和Firefox等硬编码列表,但其封闭源代码很难验证。
正如Chrome代码所表明的那样,Firefox 以类似的方式工作。从文件uriloader\exthandler\nsExternalHelperAppService.cpp
,方法摘录nsExternalHelperAppService::GetTypeFromExtension
// OK. We want to try the following sources of mimetype information, in this order:
// 1. defaultMimeEntries array
// 2. User-set preferences (managed by the handler service)
// 3. OS-provided information
// 4. our "extras" array
// 5. Information from plugins
// 6. The "ext-to-type-mapping" category
发布于 2018-05-11 20:14:54
我花了一些时间阅读RFC,MSDN和MDN。这是我能理解的。当浏览器遇到要上传的文件时,它会查看收到的第一个数据缓冲区,然后对其执行测试。这些测试尝试确定该文件是否是已知的MIME类型,如果已知MIME类型,它将进一步测试已知的MIME类型并据此采取行动。我认为IE试图首先做到这一点,而不仅仅是从扩展中确定文件类型。本页面解释了IE http://msdn.microsoft.com/en-us/library/ms775147%28v=vs.85%29.aspx。对于firefox,我能理解的是它试图从文件系统或目录项中读取文件信息,然后确定文件类型。这里是FF的链接https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIFile。我仍然希望在这方面有更多的权威信息。
https://stackoverflow.com/questions/-100008454
复制相似问题