c# - 为什么 Urlmon.dll 的 FindMimeFromData 函数会为许多文件类型返回 MIME 类型 “application/octet-stream”?

标签 c# .net mime mime-types

为什么 Urlmon.dll 的 FindMimeFromData 函数会为许多文件类型返回 MIME 类型“application/octet-stream”,而通过文件扩展名(即针对 Windows 注册表)检查 MIME 类型会返回更精确的类型?

例如,mp3 是“application/octet-stream”而不是“audio/mp3”。

基本上,我想验证一个扩展名不正确的上传文件。这种方法似乎适用于许多图像文件、xml 等。

问题类似于this one ,但提供的解决方案不适合验证上传的文件,因为返回了不同/不明确的 MIME 类型。

最佳答案

阅读 FindMimeFromData 的文档带我到MIME Type Detection in Internet Explorer .根据该信息,可以通过硬编码找到 26 different MIME types。 ,这在当今世界是相当小的。 “音频/mp3”不是其中之一。

FindMimeFromData contains hard-coded tests for (currently 26) separate MIME type (see Known MIME Types). This means that if a given buffer contains data in the format of one of these MIME types, a test exists in FindMimeFromData that is designed (by scanning through the buffer contents) to recognize the corresponding MIME type. A MIME type is known if it is one of these N MIME types. A MIME type is ambiguous if it is "text/plain," "application/octet-stream," an empty string, or null (that is, the server failed to provide it).

不幸的是,看起来 FindMimeFromData 对于确定现代 MIME 类型不是很有用。

关于c# - 为什么 Urlmon.dll 的 FindMimeFromData 函数会为许多文件类型返回 MIME 类型 “application/octet-stream”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833113/

相关文章:

c# - EntityType 'Movie_Category_Map' 没有定义键。定义此 EntityType 的键

c# - 如何从 app.config 中读取自定义 XML?

c# - 使用 AspNet 从 Azure Blob 存储下载并重命名文件

c# - Xamarin iOS 中无参数的绑定(bind)方法

c# - 合并相同类型的列表<T>

c# - 使用 JsonTextReader 值作为新流传递 Base64 编码的字符串

c# - 使用 native 加密提供程序时 .NET 中的内存泄漏

html - 正确的 MIME 类型有何重要性?

http - 获取使用 net/http request.ParseMultipartForm 处理的上传文件的 Content-Type header

java - 将 javax.mail.internet.MimeMessage 发送给非 ASCII 名称的收件人?