C# Mime Types class
Asked Answered
C

4

21

I want to save a Mime Type in my code. Now I use to do this:

string mYMimeType = "text/plain";

Is there a way to save it in a (already existent) standard,dedicated class? Something like...

Http.MimeTypes myMimeType = Http.MimeTypes.TextPlain;
Craniotomy answered 19/12, 2017 at 11:46 Comment(0)
C
35

you can make use of MediaTypeNames class exists in System.Net.Mime namesapce.

Below is .net class can help you , you dont have to create it by youself.

namespace System.Net.Mime
{
    // Summary:
    //     Specifies the media type information for an e-mail message attachment.
    public static class MediaTypeNames
    {

        // Summary:
        //     Specifies the kind of application data in an e-mail message attachment.
        public static class Application
        {
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Application data is not
            //     interpreted.
            public const string Octet = "application/octet-stream";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Application data is in
            //     Portable Document Format (PDF).
            public const string Pdf = "application/pdf";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Application data is in
            //     Rich Text Format (RTF).
            public const string Rtf = "application/rtf";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Application data is a SOAP
            //     document.
            public const string Soap = "application/soap+xml";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Application data is compressed.
            public const string Zip = "application/zip";
        }

        // Summary:
        //     Specifies the type of image data in an e-mail message attachment.
        public static class Image
        {
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Image data is in Graphics
            //     Interchange Format (GIF).
            public const string Gif = "image/gif";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Image data is in Joint
            //     Photographic Experts Group (JPEG) format.
            public const string Jpeg = "image/jpeg";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Image data is in Tagged
            //     Image File Format (TIFF).
            public const string Tiff = "image/tiff";
        }

        // Summary:
        //     Specifies the type of text data in an e-mail message attachment.
        public static class Text
        {
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Text data is in HTML format.
            public const string Html = "text/html";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Text data is in plain text
            //     format.
            public const string Plain = "text/plain";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Text data is in Rich Text
            //     Format (RTF).
            public const string RichText = "text/richtext";
            //
            // Summary:
            //     Specifies that the System.Net.Mime.MediaTypeNames.Text data is in XML format.
            public const string Xml = "text/xml";
        }
    }
}
Cuprum answered 19/12, 2017 at 11:53 Comment(4)
Why a standard like .net doesn't have all mediatypes, but only a few? I can't understand :-) However, Thanks for the reply – Craniotomy
They don't even have text/json or application/json, wow. πŸ˜• – Plerre
Oh that's not true. Since dotnet core 2.1 MediaTypeNames.Application.Json has been defined. – Iq
The MediaTypeNames class does sadly not seem to be a good choice. For example, using NET6, the media type Image only includes Gif, Jpeg and Tiff. Png is not included even though it has been a mainstream format for at least 15 years. – Onassis
W
0

You can use method to get mime type by file extension MimeTypeMapper.GetMimeType(".xlsx") Here is an example of implementation:

public static class MimeTypeMapper
{
    private static readonly IDictionary<string, string> _mappings =
        new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
        {
            {".xls", "application/vnd.ms-excel"},
            {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"},
            {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"},
            {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            {".xlt", "application/vnd.ms-excel"},
            {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"},
            {".zip", "application/x-zip-compressed"},
        };

    public static string GetMimeType(string extension)
    {
        if (extension == null)
        {
            throw new ArgumentNullException("extension");
        }
        if (!extension.StartsWith("."))
        {
            extension = "." + extension;
        }
        string mime;
        return _mappings.TryGetValue(extension, out mime) ? mime : "application/octet-stream";
    }
}
Winnifredwinning answered 28/4, 2021 at 11:44 Comment(0)
L
0

Note that the list of inbuild MimeTypes has been broadened since the answer proided Pranay Rana in https://mcmap.net/q/601242/-c-mime-types-class

Below is the current simplified list of values

namespace System.Net.Mime
{
    public static class MediaTypeNames
    {
        public static class Text
        {
            public const string Plain = "text/plain";
            public const string Html = "text/html";
            public const string Xml = "text/xml";
            public const string RichText = "text/richtext";
        }

        public static class Application
        {
            public const string Soap = "application/soap+xml";
            public const string Octet = "application/octet-stream";
            public const string Rtf = "application/rtf";
            public const string Pdf = "application/pdf";
            public const string Zip = "application/zip";
            public const string Json = "application/json";
            public const string Xml = "application/xml";
        }

        public static class Image
        {
            public const string Gif = "image/gif";
            public const string Tiff = "image/tiff";
            public const string Jpeg = "image/jpeg";
        }
    }
}
Lingual answered 9/3, 2023 at 12:9 Comment(0)
C
0

And now in NET 8 it supports more image formats including PNG:

public static class Image
{
    public const string Avif = "image/avif";
    public const string Bmp = "image/bmp";
    public const string Gif = "image/gif";
    public const string Icon = "image/x-icon";
    public const string Jpeg = "image/jpeg";
    public const string Png = "image/png";
    public const string Svg = "image/svg+xml";
    public const string Tiff = "image/tiff";
    public const string Webp = "image/webp";
}
Carcass answered 22/7 at 2:57 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.