如何是取决于浏览器上传文件的MIME类型? [英] How is mime type of an uploaded file determined by browser?

查看:273
本文介绍了如何是取决于浏览器上传文件的MIME类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里,用户需要上传一个.zip文件的Web应用程序。在服务器端,我检查上传文件的MIME类型,以确保它是应用程序/ x-ZIP-COM pressed 应用程序/压缩

I have a web app where the user needs to upload a .zip file. On the server-side, I am checking the mime type of the uploaded file, to make sure it is application/x-zip-compressed or application/zip.

这工作得很好,我在Firefox和IE浏览器。然而,当一个同事测试了它,它没有为他在Firefox(发送MIME类型是像应用程序/八位字节流),但在Internet Explorer上工作。我们的设置似乎是相同的:IE8,FF 3.5.1与所有加载项禁用,WIN XP SP3,WinRAR的安装为本地.zip文件处理程序(不知道这是有关)

This worked fine for me on Firefox and IE. However, when a coworker tested it, it failed for him on Firefox (sent mime type was something like "application/octet-stream") but worked on Internet Explorer. Our setups seem to be identical: IE8, FF 3.5.1 with all add-ons disabled, Win XP SP3, WinRAR installed as native .zip file handler (not sure if that's relevant).

所以我的问题是:如何在浏览器决定送什么MIME类型

请注意:我知道,MIME类型是由浏览器发送的,因此,不可靠的。我只是检查它的方便 - 主要是给比你试图打开一个非压缩文件为ZIP文件得到的那些更友好的错误信息,并避免加载(presumably重)拉链文件库。

推荐答案

铬的(如书写的38版)的有3种方式来确定的MIME类型和以一定的顺序这样做。下面的代码段是从文件的src /网/基/ mime_util.cc ,方法 MimeUtil :: GetMimeTypeFromExtensionHelper

Chrome

Chrome (version 38 as of writing) has 3 ways to determine the MIME type and does so in a certain order. The snippet below is from file src/net/base/mime_util.cc, method 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.

在硬件codeD列出来的文件中较早的一个位:<一href=\"https://$c$c.google.com/p/chromium/$c$csearch#chromium/src/net/base/mime_util.cc&l=201\">https://$c$c.google.com/p/chromium/$c$csearch#chromium/src/net/base/mime_util.cc&l=201 ( primary_mappings secondary_mappings )。

一个例子:从Windows系统中使用Microsoft Excel安装上传CSV文件时,浏览器会报告这个作为应用程序/ vnd.ms - Excel中。这是因为的.csv 第一硬codeD列表中没有指定,所以浏览器回落到系统注册表。 HKEY_CLASSES_ROOT \\的.csv 有一个名为内容类型的值设置为应用程序/越南盾。 MS-Excel中

An example: when uploading a CSV file from a Windows system with Microsoft Excel installed, Chrome will report this as application/vnd.ms-excel. This is because .csv is not specified in the first hard-coded list, so the browser falls back to the system registry. HKEY_CLASSES_ROOT\.csv has a value named Content Type that is set to application/vnd.ms-excel.

再次使用相同的例子,浏览器会报告应用程序/ vnd.ms - Excel中。我认为这是合理的假设Internet Explorer中的(11写的版本)的使用注册表。也许这也使得喜欢使用Chrome和Firefox,但它的封闭源代码的性质硬codeD列表使得它难以核实。

Again using the same example, the browser will report application/vnd.ms-excel. I think it's reasonable to assume Internet Explorer (version 11 as of writing) uses the registry. Possibly it also makes use of a hard-coded list like Chrome and Firefox, but its closed source nature makes it hard to verify.

如在铬code所示,火狐的(写作的版本32)的工作方式类似。从文件片段uriloader \\ exthandler \\ nsExternalHelperAppService.cpp ,方法 nsExternalHelperAppService :: GetTypeFromExtension

As indicated in the Chrome code, Firefox (version 32 as of writing) works in a similar way. Snippet from file uriloader\exthandler\nsExternalHelperAppService.cpp, method 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

在硬件codeD列表文件中的提前到来,附近的某处线441.您正在寻找 defaultMimeEntries extraMimeEntries

使用我的当前配置文件时,浏览器会报告文本/ CSV ,因为有一个在它的入口 mimeTypes.rdf (上述第2项中的列表)。用新鲜的配置文件,它没有这个入口,浏览器会报告应用程序/ vnd.ms - Excel中(列表中的第3项)。

With my current profile, the browser will report text/csv because there's an entry for it in mimeTypes.rdf (item 2 in the list above). With a fresh profile, which does not have this entry, the browser will report application/vnd.ms-excel (item 3 in the list).

在浏览器中的硬codeD名单是pretty有限。通常情况下,通过浏览器发送的MIME类型将是一个由OS报告。这就是为什么,如问题所说,由浏览器报告的MIME类型是不可靠的。

The hard-coded lists in the browsers are pretty limited. Often, the MIME type sent by the browser will be the one reported by the OS. And this is exactly why, as stated in the question, the MIME type reported by the browser is unreliable.

这篇关于如何是取决于浏览器上传文件的MIME类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆