Using .NET, how can you find the mime type of a file based on the file signature not the extension
c#, mime, mime-types
Solution
In Urlmon.dll, there's a function called `FindMimeFromData`.
From the documentation
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.
So, read the first (up to) 256 bytes from the file and pass it to `FindMimeFromData`.
Problem
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.
Related problems
- What is a correct MIME type for .docx, .pptx, etc.?
- urlmon.dll FindMimeFromData() works perfectly on 64bit desktop/console but generates errors on ASP.NET
- Alternative to FindMimeFromData method in Urlmon.dll one which has more MIME types
- how to identify the extension/type of the file using C#?
- Why does the FindMimeFromData function from Urlmon.dll return MIME type “application/octet-stream” for many file types?
- How to get a file's Media Type (MIME type)?
- Is there a way to infer what image format a file is, without reading the entire file?