i want to find the mime-type for a given file extension on an IIS ASP.NET web-server from the code-behind file.
我想在IIS ASP上找到给定文件扩展名的mime类型。NET web服务器来自代码隐藏文件。
i want to search the same list that the server itself uses when serving up a file. This means that any mime types a web-server administrator has added to the Mime Map will be included.
我希望搜索服务器在提供文件时使用的列表。这意味着将包括web服务器管理员添加到mime映射的任何mime类型。
i could blindly use
我可以盲目使用
HKEY_CLASSES_ROOT\MIME\Database\Content Type
but that isn't documented as being the same list IIS uses, nor is it documented where the Mime Map is stored.
但这并没有记录为IIS使用的列表,也没有记录到Mime映射存储的位置。
i could blindly call FindMimeFromData, but that isn't documented as being the same list IIS uses, nor can i guarantee that the IIS Mime Map will also be returned from that call.
我可以盲目地调用FindMimeFromData,但这并没有记录为IIS使用的相同列表,我也不能保证IIS Mime映射也会从该调用中返回。
3 个解决方案
#1
10
Here's one I made earlier:
这是我之前做的一个:
public static string GetMimeTypeFromExtension(string extension)
{
using (DirectoryEntry mimeMap =
new DirectoryEntry("IIS://Localhost/MimeMap"))
{
PropertyValueCollection propValues = mimeMap.Properties["MimeMap"];
foreach (object value in propValues)
{
IISOle.IISMimeType mimeType = (IISOle.IISMimeType)value;
if (extension == mimeType.Extension)
{
return mimeType.MimeType;
}
}
return null;
}
}
Add a reference to System.DirectoryServices
and a reference to Active DS IIS Namespace Provider
under the COM tab. The extension needs to have the leading dot, i.e. .flv
.
向系统添加引用。DirectoryServices和COM选项卡下的活动DS IIS命名空间提供程序的引用。扩展需要有前导点,即。flv。
#2
12
Here is another similar implementation, but doesn't require adding the COM reference - it retrieves the properties through reflection instead and stores them in a NameValueCollection for easy lookup:
这里是另一个类似的实现,但不需要添加COM引用——它通过反射来检索属性,并将它们存储在一个NameValueCollection中,以便查找:
using System.Collections.Specialized; //NameValueCollection
using System.DirectoryServices; //DirectoryEntry, PropertyValueCollection
using System.Reflection; //BindingFlags
NameValueCollection map = new NameValueCollection();
using (DirectoryEntry entry = new DirectoryEntry("IIS://localhost/MimeMap"))
{
PropertyValueCollection properties = entry.Properties["MimeMap"];
Type t = properties[0].GetType();
foreach (object property in properties)
{
BindingFlags f = BindingFlags.GetProperty;
string ext = t.InvokeMember("Extension", f, null, property, null) as String;
string mime = t.InvokeMember("MimeType", f, null, property, null) as String;
map.Add(ext, mime);
}
}
You can very easily cache that lookup table, and then reference it later:
您可以很容易地缓存查找表,然后在以后引用它:
Response.ContentType = map[ext] ?? "binary/octet-stream";
#3
1
IIS stores the MIME information in its own database. Searching for "MimeMap IIS" on the internet will reveal how to read it or even change it. See for example C# - How to display MimeMap entries to the console from an instance of IIS.
IIS将MIME信息存储在自己的数据库中。在互联网上搜索“MimeMap IIS”将揭示如何阅读它,甚至是修改它。请参见示例c# -如何将MimeMap条目从IIS实例显示到控制台。
#1
10
Here's one I made earlier:
这是我之前做的一个:
public static string GetMimeTypeFromExtension(string extension)
{
using (DirectoryEntry mimeMap =
new DirectoryEntry("IIS://Localhost/MimeMap"))
{
PropertyValueCollection propValues = mimeMap.Properties["MimeMap"];
foreach (object value in propValues)
{
IISOle.IISMimeType mimeType = (IISOle.IISMimeType)value;
if (extension == mimeType.Extension)
{
return mimeType.MimeType;
}
}
return null;
}
}
Add a reference to System.DirectoryServices
and a reference to Active DS IIS Namespace Provider
under the COM tab. The extension needs to have the leading dot, i.e. .flv
.
向系统添加引用。DirectoryServices和COM选项卡下的活动DS IIS命名空间提供程序的引用。扩展需要有前导点,即。flv。
#2
12
Here is another similar implementation, but doesn't require adding the COM reference - it retrieves the properties through reflection instead and stores them in a NameValueCollection for easy lookup:
这里是另一个类似的实现,但不需要添加COM引用——它通过反射来检索属性,并将它们存储在一个NameValueCollection中,以便查找:
using System.Collections.Specialized; //NameValueCollection
using System.DirectoryServices; //DirectoryEntry, PropertyValueCollection
using System.Reflection; //BindingFlags
NameValueCollection map = new NameValueCollection();
using (DirectoryEntry entry = new DirectoryEntry("IIS://localhost/MimeMap"))
{
PropertyValueCollection properties = entry.Properties["MimeMap"];
Type t = properties[0].GetType();
foreach (object property in properties)
{
BindingFlags f = BindingFlags.GetProperty;
string ext = t.InvokeMember("Extension", f, null, property, null) as String;
string mime = t.InvokeMember("MimeType", f, null, property, null) as String;
map.Add(ext, mime);
}
}
You can very easily cache that lookup table, and then reference it later:
您可以很容易地缓存查找表,然后在以后引用它:
Response.ContentType = map[ext] ?? "binary/octet-stream";
#3
1
IIS stores the MIME information in its own database. Searching for "MimeMap IIS" on the internet will reveal how to read it or even change it. See for example C# - How to display MimeMap entries to the console from an instance of IIS.
IIS将MIME信息存储在自己的数据库中。在互联网上搜索“MimeMap IIS”将揭示如何阅读它,甚至是修改它。请参见示例c# -如何将MimeMap条目从IIS实例显示到控制台。