C# Mime 类型类
Posted
技术标签:
【中文标题】C# Mime 类型类【英文标题】:C# Mime Types class 【发布时间】:2018-06-01 20:22:28 【问题描述】:我想在我的代码中保存一个 Mime 类型。 现在我习惯这样做:
string mYMimeType = "text/plain";
有没有办法将它保存在(已经存在的)标准的专用类中? 比如……
Http.MimeTypes myMimeType = Http.MimeTypes.TextPlain;
【问题讨论】:
【参考方案1】:你可以利用MediaTypeNames
类存在于System.Net.Mime
命名空间。
下面是.net类可以帮助你,你不必自己创建它。
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";
【讨论】:
为什么像 .net 这样的标准没有所有的媒体类型,而只有少数?我不明白 :-) 不过,谢谢你的回复 他们甚至没有text/json
或application/json
,哇。 ?
哦,这不是真的。由于 dotnet core 2.1 MediaTypeNames.Application.Json 已被定义。【参考方案2】:
您可以使用方法通过文件扩展名MimeTypeMapper.GetMimeType(".xlsx")
获取mime类型
下面是一个实现示例:
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";
【讨论】:
以上是关于C# Mime 类型类的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 RestSharp 和 GoCardless 设置 MIME 类型
未找到 Java 类型、类 org.json.JSONObject.... 和 MIME 媒体类型、application/json 的消息正文阅读器
未找到 Java 类 java.util.ArrayList、Java 类型类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器