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

发布于 2024-10-14 13:25:32 字数 434 浏览 6 评论 0原文

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

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

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

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

Why does the FindMimeFromData function from Urlmon.dll return MIME type “application/octet-stream” for many file types, whereas checking MIME type by file extension (I.e. against windows registry) returns a more precise type?

For example, mp3 is an “application/octet-stream” instead of “audio/mp3”.

Basically, I want to verify an uploaded file with incorrect extension. This method seems to work for many image files, xml, etc.

The question is similar to this one, but the provided solution is not suited for validating uploaded files, because of different/ambiguous MIME types returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趴在窗边数星星i 2024-10-21 13:25:32

阅读 FindMimeFromData 的文档引导我到 Internet Explorer 中的 MIME 类型检测。根据该信息,硬编码查找 26不同的 MIME 类型,这在当今世界是相当小的。 “音频/mp3”不是其中之一。

FindMimeFromData 包含针对(当前 26 个)单独 MIME 类型的硬编码测试(请参阅已知 MIME 类型)。这意味着,如果给定缓冲区包含这些 MIME 类型之一格式的数据,则 FindMimeFromData 中存在一个测试,该测试旨在(通过扫描缓冲区内容)来识别相应的 MIME 类型。如果 MIME 类型是这 N 个 MIME 类型之一,则该 MIME 类型是已知的。如果 MIME 类型是“text/plain”、“application/octet-stream”、空字符串或 null(即服务器无法提供它),则该类型是不明确的。

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

Reading the documentation for FindMimeFromData lead me to MIME Type Detection in Internet Explorer. According to that information it is hard-coded to find 26 different MIME types, which is quite small in today's world. "audio/mp3" is not one of them.

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).

Unfortunately, it looks like FindMimeFromData won't be very useful for determining modern MIME types.

风柔一江水 2024-10-21 13:25:32

这里有一个很好的解决方案: https://stackoverflow.com/a/9435701/74585 尝试确定文件类型如果 FindMimeFromData 返回“application/octet-stream”“text/plain”或根本不返回任何内容,则从注册表(通过文件扩展名?)。

这是一个 .NET 文件类型检测库 http://filetypeDetective.codeplex.com/ 但它只检测目前文件数量很少。

There's a nice solution here: https://stackoverflow.com/a/9435701/74585 which tries to determine the file type from the registry (by file extension?) if FindMimeFromData returns "application/octet-stream" "text/plain" or nothing at all.

Here's a .NET file type detecting library http://filetypedetective.codeplex.com/ but it only detects a smallish number of files at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文