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;
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;
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";
}
}
}
text/json
or application/json
, wow. π β
Plerre 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 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";
}
}
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";
}
}
}
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";
}
© 2022 - 2024 β McMap. All rights reserved.