Get MIME type from filename extension(从文件扩展名中获取 MIME 类型)
问题描述
How can I get the MIME type from a file extension?
For ASP.NET or other
The options were changed a bit in ASP.NET Core, here they are (credits):
new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);(vNext only)- Never tested, but looks like you can officially expand the mime types list via the exposed
Mappingsproperty.
- Never tested, but looks like you can officially expand the mime types list via the exposed
- Use the
MimeTypesNuGet package - Copy the
MimeMappingsfile from the reference source of the .NET Framework
For .NET Framework >= 4.5:
Use the System.Web.MimeMapping.GetMimeMapping method, that is part of the BCL in .NET Framework 4.5:
string mimeType = MimeMapping.GetMimeMapping(fileName);
If you need to add custom mappings you probably can use reflection to add mappings to the BCL MimeMapping class, it uses a custom dictionary that exposes this method, so you should invoke the following to add mappings (never tested tho, but should prob. work).
Anyway, when using reflection to add MIME types, be aware that since you're accessing a private field, its name might change or even be totally removed, so you should be extra cautious and add double checks and provide fail safe action for every step.
MimeMapping._mappingDictionary.AddMapping(string fileExtension, string mimeType)
这篇关于从文件扩展名中获取 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从文件扩展名中获取 MIME 类型
基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
