如何从文件扩展名中获得MIME类型?
当前回答
IANA媒体类型
我希望微软能把他们的行业标准统一起来!对于其他人,有兴趣的人:
离散的类型
application: https://www.iana.org/assignments/media-types/media-types.xhtml#application audio: https://www.iana.org/assignments/media-types/media-types.xhtml#audio example: https://www.iana.org/assignments/media-types/media-types.xhtml#examples font: https://www.iana.org/assignments/media-types/media-types.xhtml#font image: https://www.iana.org/assignments/media-types/media-types.xhtml#image model: https://www.iana.org/assignments/media-types/media-types.xhtml#model text: https://www.iana.org/assignments/media-types/media-types.xhtml#text video: https://www.iana.org/assignments/media-types/media-types.xhtml#video
多部分类型
信息:https://www.iana.org/assignments/media-types/media-types.xhtml消息 多部分:https://www.iana.org/assignments/media-types/media-types.xhtml #多部分
我想推荐一个阅读:MIME类型(IANA媒体类型)的Mozilla页面感兴趣的人!它的信息量很大!
在代码方面,上面的每个链接都有一个.csv文件下载:https://www.iana.org/assignments/media-types/application.csv
正如这里已经指出的,可以使用Dictionary或ConcurrentDictionary下载并使用Key - Value对填充Dictionary。
其他回答
.NET Core获取MimeType的方法:
添加依赖关系
Microsoft.AspNetCore.StaticFiles
private string GetMimeType(string fileName)
{
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(fileName, out var contentType))
{
contentType = "application/octet-stream";
}
return contentType;
}
您可以使用Apache的httpd提供的表。将其映射到函数、字典、列表等应该是很简单的。
另外,正如这里所看到的,extension->mime类型不一定是函数。每个文件扩展名可能有多种常见的MIME类型,所以你应该看看你的应用程序的需求,看看你为什么关心MIME类型,你想用它们“做什么”等等。您可以使用文件扩展名来确定相同的行为吗?您是否也需要读取文件的前几个字节来确定其MIME类型?
如果有人能在linux上使用libmagic类似的功能,那就更好了,因为我认为这是一种比依赖文件扩展名更好的检测文件类型的方法。
例如,如果我将一个文件从myppicture .jpg重命名为myppicture .txt 在linux上,它仍然会被报告为一张图片 但是在这里使用这种方法,它将被报告为文本文件。
目光Tomas
为了使这篇文章更全面,对于。net核心开发人员有FileExtensionContentTypeProvider类,它涵盖了官方的MIME内容类型。
它在幕后工作——根据文件扩展名在Http响应头中设置ContentType。
如果您需要特殊的MIME类型,请参阅自定义MIME类型的示例:
public void Configure(IApplicationBuilder app)
{
// Set up custom content types -associating file extension to MIME type
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".myapp"] = "application/x-msdownload";
provider.Mappings[".htm3"] = "text/html";
provider.Mappings[".image"] = "image/png";
// Replace an existing mapping
provider.Mappings[".rtf"] = "application/x-msdownload";
// Remove MP4 videos.
provider.Mappings.Remove(".mp4");
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
RequestPath = new PathString("/MyImages"),
ContentTypeProvider = provider
});
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
RequestPath = new PathString("/MyImages")
});
}
Bryan Denny上面的帖子不适合我,因为不是所有的扩展在注册表中都有一个“内容类型”子键。我不得不调整代码如下:
private string GetMimeType(string sFileName)
{
// Get file extension and if it is empty, return unknown
string sExt = Path.GetExtension(sFileName);
if (string.IsNullOrEmpty(sExt)) return "Unknown file type";
// Default type is "EXT File"
string mimeType = string.Format("{0} File", sExt.ToUpper().Replace(".", ""));
// Open the registry key for the extension under HKEY_CLASSES_ROOT and return default if it doesn't exist
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(sExt);
if (regKey == null) return mimeType;
// Get the "(Default)" value and re-open the key for that value
string sSubType = regKey.GetValue("").ToString();
regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(sSubType);
// If it exists, get the "(Default)" value of the new key
if (regKey?.GetValue("") != null) mimeType = regKey.GetValue("").ToString();
// Return the value
return mimeType;
}
现在它可以为我所有注册的文件类型和未注册的或通用的文件类型(如JPG等)。
推荐文章
- 最好的方法在asp.net强制https为整个网站?
- 在ASP .NET身份声明是什么
- 将字符串转换为System.IO.Stream
- 如何从枚举中选择一个随机值?
- 驻留在App_Code中的类不可访问
- 在链式LINQ扩展方法调用中等价于'let'关键字的代码
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 无法启动IIS Express Web服务器,注册URL失败,访问被拒绝
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- ASP。NET Identity DbContext混淆
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"
- 从另一个列表id中排序一个列表