使用 .NET,如何根据文件签名而不是扩展名找到文件的 MIME 类型 [英] Using .NET, how can you find the mime type of a file based on the file signature not the extension
问题描述
我正在寻找一种简单的方法来获取文件扩展名不正确或未给出的 mime 类型,类似于 这个问题 仅在 .Net 中.
I am looking for a simple way to get a mime type where the file extension is incorrect or not given, something similar to this question only in .Net.
推荐答案
在 Urlmon.dll 中,有一个名为 FindMimeFromData
的函数.
In Urlmon.dll, there's a function called FindMimeFromData
.
来自文档
MIME 类型检测或数据嗅探"是指从二进制数据中确定合适的 MIME 类型的过程.最终结果取决于服务器提供的 MIME 类型标头、文件扩展名和/或数据本身的组合.通常,只有前 256 个字节的数据是重要的.
MIME type detection, or "data sniffing," refers to the process of determining an appropriate MIME type from binary data. The final result depends on a combination of server-supplied MIME type headers, file extension, and/or the data itself. Usually, only the first 256 bytes of data are significant.
因此,从文件中读取前(最多)256 个字节并将其传递给 FindMimeFromData
.
So, read the first (up to) 256 bytes from the file and pass it to FindMimeFromData
.
这篇关于使用 .NET,如何根据文件签名而不是扩展名找到文件的 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!