[Notes] C # how to obtain the file MIME Type

Why was MIME Type:
MIME Reference Manual
svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

Conventional way

For file extensions, you can use MimeMapping.GetMimeMapping get.

MimeMapping.GetMimeMapping(String) Method (System.Web) | Microsoft Docs

If MimeMapping.GetMimeMapping do not know, it will return to application/octet-streamthe default values.

Otherwise

For certain types of files, other means may be used therewith obtain related, such as Image can be obtained in this manner:

public bool TryBuildFileMimeType(string filePath, out string mimeType)
{
    if (string.IsNullOrWhiteSpace(filePath) || !System.IO.File.Exists(filePath))
    {
        mimeType = string.Empty;
        return false;
    }
    try
    {
        var image = Image.FromFile(filePath);
        mimeType = GetMimeTypeFromImage(image);
        return !string.IsNullOrWhiteSpace(mimeType);
    }
    catch (Exception ex)
    {
        mimeType = string.Empty;
        return false;
    }
}

private string GetMimeTypeFromImage(Image image)
{
    if (image.RawFormat.Equals(ImageFormat.Jpeg))
        return "image/jpeg";
    else if (image.RawFormat.Equals(ImageFormat.Bmp))
        return "image/bmp";
    else if (image.RawFormat.Equals(ImageFormat.Emf))
        return "image/emf";
    else if (image.RawFormat.Equals(ImageFormat.Exif))
        return "image/exif";
    else if (image.RawFormat.Equals(ImageFormat.Gif))
        return "image/gif";
    else if (image.RawFormat.Equals(ImageFormat.Icon))
        return "image/icon";
    else if (image.RawFormat.Equals(ImageFormat.Png))
        return "image/png";
    else if (image.RawFormat.Equals(ImageFormat.Tiff))
        return "image/tiff";
    else if (image.RawFormat.Equals(ImageFormat.Wmf))
        return "image/wmf";
    return string.Empty;
}

In my actual scene here, most of the files have extension, which can be treated with MimeMapping, for there is no extension, are image files, can be handled in this way behind.

Of course, also according to the contents of the file header, first obtain the file type, to find the corresponding MIME Type. But the need to maintain their own table a document header identified do not know if you can use ready-made NUGET, seeking recommendations.

Related Tools

5 Tools To Help Identify Unrecognized or Unknown File Types • Raymond.CC

ExifTool This tool is very powerful, you can see a lot of file metadata information, there are command-line version and the GUI version.

ExifTool by Phil Harvey
ExifToolGUI

other

See How the Determine File of the type the without the I CAN AN ON Windows Extension -? Super the User ,
have a question, get a file based on the file content type / MIME type, essentially do not fly? You can only rely on guess? But for most common file types, there is a fixed format it?

After all, what is the content of the document, developers can be arbitrarily controlled.

Reference links or related links:

c# - Get ImageFormat from System.Drawing.Image.RawFormat - Stack Overflow
c# - Guessing a file type based on its content - Code Review Stack Exchange
.NET获取文件的MIME类型(Content Type)
Get a File Content-Type / MIME-type from file extension in ASP.NET C#
NuGet Gallery | MimeMapping 1.0.1.17

Original link:
https://www.cnblogs.com/jasongrass/p/11635454.html

Guess you like

Origin www.cnblogs.com/jasongrass/p/11635454.html