.docx、.pptx 等的正确 MIME 类型是什么?
对于较旧的 *.doc 文档,这已经足够了:
header("Content-Type: application/msword");
What MIME type should I use for new .docx document?另外,对于 pptx 和 xlsx 文档?
For older *.doc documents, this was enough:
header("Content-Type: application/msword");
What MIME type should I use for new .docx documents? Also, for pptx and xlsx documents?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
以下是 HTTP 内容流的正确 Microsoft Office MIME 类型:
有关更多详细信息,请查看此 TechNet 文章 和此 博客文章。
Here are the correct Microsoft Office MIME types for HTTP content streaming:
For further details check out this TechNet article and this blog post.
这是一个(几乎)完整的 JSON 格式的文件扩展名 MIME。
您可以执行以下示例:
MIME["ppt"]
、MIME["docx"]
等编辑:
根据@Octo Poulos建议:
video/mp4
Here is an (almost) complete file extensions's MIME in a JSON format.
You can do these examples:
MIME["ppt"]
,MIME["docx"]
, etcEdit:
According to @Octo Poulos suggestions:
video/mp4
或者,如果您使用 .NET v4.5 或更高版本,请尝试使用
System.Web.MimeMapping.GetMimeMapping
(yourFileName)
获取 MIME 类型。它比硬编码字符串要好得多。更新:在.Net Core中,可以使用
FileExtensionContentTypeProvider.TryGetContentType()
Alternatively, if you're working in .NET v4.5 or above, try using
System.Web.MimeMapping.GetMimeMapping
(yourFileName)
to get MIME types. It is much better than hard-coding strings.UPDATE: In .Net Core, the same can be achieved using
FileExtensionContentTypeProvider.TryGetContentType()
这篇文章将探讨跨各种编程语言获取 MIME 类型的各种方法,并以一行描述作为标题。因此,请相应地使用它们并选择适合您的。
例如。当用户可能提供 .xls、.xlsx 或 .xlsm 且您不想编写代码测试扩展并为每个文件提供 MIME 类型时,下面的代码特别有用。让系统来做这个工作。
Python 3
使用 python-magic
使用内置 mimeypes 模块 - 将文件名映射到 MimeTypes 模块
JAVA 7
来源:Baeldung 关于 Java 中的文件 MIME 类型的博客
操作系统相关
它将使用 FileTypeDetector 实现来探测 MIME 类型,并调用每个实现的probeContentType 来解析类型。因此,如果实现已知该文件,则返回内容类型。但是,如果没有发生,则会调用系统默认的文件类型检测器。
使用输入流的前几个字符进行解析
使用内置的 MIME 类型表
它返回 URLConnection 的所有实例使用的 MIME 类型矩阵,然后使用该矩阵来解析输入文件类型。然而,当涉及到 URLConnection 时,这个 MIME 类型矩阵非常有限。
默认情况下,该类使用 JRE_HOME/lib 中的 content-types.properties 文件。但是,我们可以通过使用 content.types.user.table 属性指定特定于用户的表来扩展它:
JavaScript
源: FileReader API & Medium 有关使用 Magic 的文章JavaScript 中的数字获取 Mime 类型
解释使用 FileReader API 获取的幻数
当使用 javaScript 基于文件流获取 MimeType 时,最终结果看起来像这样。 打开嵌入式 jsFiddle 来查看并理解这种方法。
奖励:大多数 MIME 类型都可以使用它,您还可以在 getMimetype 函数中添加自定义 Mime 类型。此外,它还完全支持 MS Office 文件 Mime 类型。
在此示例中计算文件的 MIME 类型的步骤是:
切片法。
创建一个 TypedArray,在本例中为 Uint8Array。
十六进制(通过使用 toString(16))。
浏览器支持(总体超过 95%,在所有现代浏览器中接近 100%):

This post will explore various approaches of fetching MIME Type across various programming languages with their CONS in one-line description as header. So, use them accordingly and the one which works for you.
For eg. the code below is especially helpful when user may supply either of .xls, .xlsx or .xlsm and you don't want to write code testing extension and supplying MIME-type for each of them. Let the system do this job.
Python 3
Using python-magic
Using built-in mimeypes module - Map filenames to MimeTypes modules
JAVA 7
Source: Baeldung's blog on File MIME Types in Java
Operating System dependent
It will use FileTypeDetector implementations to probe the MIME type and invokes the probeContentType of each implementation to resolve the type. Hence, if the file is known to the implementations then the content type is returned. However, if that doesn’t happen, a system-default file type detector is invoked.
Resolve using first few characters of the input stream
Using built-in table of MIME types
It returns the matrix of MIME types used by all instances of URLConnection which then is used to resolve the input file type. However, this matrix of MIME types is very limited when it comes to URLConnection.
By default, the class uses content-types.properties file in JRE_HOME/lib. We can, however, extend it, by specifying a user-specific table using the content.types.user.table property:
JavaScript
Source: FileReader API & Medium's article on using Magic Numbers in JavaScript to get Mime Types
Interpret the Magic Number fetched using FileReader API
Final result looks something like this when one use javaScript to fetch the MimeType based on filestream. Open the embedded jsFiddle to see and understand this approach.
Bonus: It's accessible for most of the MIME Types and also you can add custom Mime Types in the getMimetype function. Also, it has FULL SUPPORT for MS Office Files Mime Types.
The steps to calculate mime type for a file in this example would be:
slice method.
create a TypedArray, in this case an Uint8Array.
hexadecimal (by using toString(16)).
Browser Support (Above 95% overall and Close to 100% in all modern browsers):

android 中填充映射列表 mime 类型的工作方法。
A working method in android to populates the mapping list mime types.
如果有人想要 C# switch 语句中的 Dirk Vollmar 的答案:
In case anyone wants the answer of Dirk Vollmar in a C# switch statement:
斯威夫特4
Swift4
WPS 电子表格:
WPS spreadsheet :
要加载 .docx 文件:
To load a .docx file:
但如果您(或您的客户)有 WPS,也许您需要添加
but if you (or your client) has WPS, maybe you need add