文件扩展名和MIME类型在.NET [英] File extensions and MIME Types in .NET
问题描述
我希望得到一个MIME内容类型从给定的扩展名(preferably,而无需访问物理文件)。我已经看到了这个问题的一些说明,并执行此方法可以在恢复:
I want to get a MIME Content-Type from a given extension (preferably without accessing the physical file). I have seen some questions about this and the methods described to perform this can be resumed in:
- 使用<一个href="http://stackoverflow.com/questions/1029740/get-a-mime-from-an-extention/1029796#1029796">registry信息。
- 使用<一个href="http://stackoverflow.com/questions/58510/in-c-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature-not-th/58570#58570">urlmon.dll's FindMimeFromData A>。
- 使用<一个href="http://stackoverflow.com/questions/174888/asp-net-iis6-how-to-search-the-servers-mime-map/174988#174988">IIS信息。
- 滚你自己的MIME映射功能。基于此表,例如。
- Use registry information.
- Use urlmon.dll's FindMimeFromData.
- Use IIS information.
- Roll your own MIME mapping function. Based on this table, for example.
我一直在使用一号一段时间,但我意识到,注册表提供的信息并不一致,取决于安装在计算机上的软件。某些扩展,如.ZIP不要用有指定的内容类型。
I've been using no.1 for some time but I realized that the information provided by the registry is not consistent and depends on the software installed on the machine. Some extensions, like .zip don't use to have a Content-Type specified.
解决方案二号迫使我对磁盘上的文件,以便读取第一个字节,这是一件好事缓慢,但可能会得到很好的效果。
Solution no.2 forces me to have the file on disk in order to read the first bytes, which is something slow but may get good results.
第三种方法是基于目录服务和所有的东西,这是我不喜欢的,因为我必须添加COM引用,我不知道这是IIS6和IIS7之间是一致的。另外,我不知道该方法的性能。
The third method is based on Directory Services and all that stuff, which is something I don't like much because I have to add COM references and I'm not sure it's consistent between IIS6 and IIS7. Also, I don't know the performance of this method.
最后,我不想用我自己的表,但最后似乎是最好的选择,如果我想要的结果的平台(甚至是单声道)之间的不俗的性能和一致性。
Finally, I didn't want to use my own table but at the end seems the best option if I want a decent performance and consistency of the results between platforms (even Mono).
你觉得还有比用我自己的表或其他描述的方法之一是更好的是更好的选择?你对此有何经验?
Do you think there's a better option than using my own table or one of other described methods are better? What's your experience?
推荐答案
这取决于你所需要的MIME类型。在一般情况下,服务(Web应用程序,Web服务等),这是最好不要使用MIME列表,它是依赖于操作系统的设置,或只是作为后备,如果你不能找到MIME信息除外。
It depends what you need the MIME type for. In general, for services (web apps, web service, etc.), it's advisable not to use a MIME list which is dependent on the OS settings, or only as fallback if you cannot find MIME information otherwise.
我认为,这也是为什么MS选择了把恒定的MIME类型的System.Web.MimeMapping类的原因(不幸的是它的内部,无论出于何种原因)。
I think that this is also the reason why MS chose to put constant MIME types in their System.Web.MimeMapping class (unfortunately it's internal, for whatever reason).
编辑:
public static class MimeExtensionHelper
{
static object locker = new object();
static object mimeMapping;
static MethodInfo getMimeMappingMethodInfo;
static MimeExtensionHelper()
{
Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
if (mimeMappingType == null)
throw new SystemException("Couldnt find MimeMapping type");
ConstructorInfo constructorInfo = mimeMappingType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
if (constructorInfo == null)
throw new SystemException("Couldnt find default constructor for MimeMapping");
mimeMapping = constructorInfo.Invoke(null);
if (mimeMapping == null)
throw new SystemException("Couldnt find MimeMapping");
getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
if (getMimeMappingMethodInfo == null)
throw new SystemException("Couldnt find GetMimeMapping method");
if (getMimeMappingMethodInfo.ReturnType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid return type");
if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid parameters");
}
public static string GetMimeType(string filename)
{
lock (locker)
return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
}
}
包装(.NET 4.0)
public static class MimeExtensionHelper
{
static object locker = new object();
static object mimeMapping;
static MethodInfo getMimeMappingMethodInfo;
static MimeExtensionHelper()
{
Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
if (mimeMappingType == null)
throw new SystemException("Couldnt find MimeMapping type");
getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
if (getMimeMappingMethodInfo == null)
throw new SystemException("Couldnt find GetMimeMapping method");
if (getMimeMappingMethodInfo.ReturnType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid return type");
if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid parameters");
}
public static string GetMimeType(string filename)
{
lock (locker)
return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
}
}
.NET 4.5 +
没有包装要求,调用公共方法 System.Web.MimeMapping.GetMimeMapping
直接
No wrapper requires, call public method System.Web.MimeMapping.GetMimeMapping
directly.
这篇关于文件扩展名和MIME类型在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!