我想填充有沒有辦法讓所有的MIME類型,而不是打破一個巨大的病例陳述?
Response.ContentType = "text/plain";
從某處的服務器/網絡/字典ALL可能的MIME類型按照文件的擴展名:
public string GetMimeType(string extension)
{
//This is what I am looking for.
}
另外,我要重命名文件(在至少如果要下載,所以我必須提前知道它是否會被打開。
我想填充有沒有辦法讓所有的MIME類型,而不是打破一個巨大的病例陳述?
Response.ContentType = "text/plain";
從某處的服務器/網絡/字典ALL可能的MIME類型按照文件的擴展名:
public string GetMimeType(string extension)
{
//This is what I am looking for.
}
另外,我要重命名文件(在至少如果要下載,所以我必須提前知道它是否會被打開。
可以存儲MIME類型時,文件上傳和發送,當被請求的文件。
嗯......爲什麼?你不打算返回任何類型的內容,是嗎?
以下是常見的列表類型:http://www.webmaster-toolkit.com/mime-types.shtml。沒有包含「ALL」類型的列表,因爲任何應用程序供應商都可以創建一個自定義的並將其與自定義擴展相關聯。
這將取決於您的平臺。這裏有一個C#和IIS:http://blog.crowe.co.nz/archive/2006/06/02/647.aspx
在PowerShell中它是一個班輪:(文件上傳PostedFile.ContentType)
([adsi]"IIS://localhost/MimeMap").MimeMap
在發表理查德鏈接代碼:
// Maintain a sorted list to contain the MIME Types
SortedList sl = new SortedList();
Console.WriteLine("IIS Mime Map - c#");
Console.WriteLine();
// Serve to connect to...
string ServerName = "LocalHost";
// Define the path to the metabase
string MetabasePath = "IIS://" + ServerName + "/MimeMap";
// Note: This could also be something like
// string MetabasePath = "IIS://" + ServerName + "/w3svc/1/root";
try
{
// Talk to the IIS Metabase to read the MimeMap Metabase key
DirectoryEntry MimeMap = new DirectoryEntry(MetabasePath);
// Get the Mime Types as a collection
PropertyValueCollection pvc = MimeMap.Properties["MimeMap"];
// Add each Mime Type so we can display it sorted later
foreach (object Value in pvc)
{
// Convert to an IISOle.MimeMap - Requires a connection to IISOle
// IISOle can be added to the references section in VS.NET by selecting
// Add Reference, selecting the COM Tab, and then finding the
// Active DS Namespace provider
IISOle.MimeMap mimetypeObj = (IISOle.MimeMap)Value;
// Add the mime extension and type to our sorted list.
sl.Add(mimetypeObj.Extension, mimetypeObj.MimeType);
}
// Render the sorted MIME entries
if (sl.Count == 0)
Console.WriteLine("No MimeMap entries are defined at {0}!", MetabasePath);
else
foreach (string Key in sl.Keys)
Console.WriteLine("{0} : {1}", Key.PadRight(20), sl[Key]);
}
catch (Exception ex)
{
if ("HRESULT 0x80005006" == ex.Message)
Console.WriteLine(" Property MimeMap does not exist at {0}", MetabasePath);
else
Console.WriteLine("An exception has occurred: \n{0}", ex.Message);
}
//轉換到IISOle.MimeMap - 需要
// IISOle可以添加到VS.NET中引用部分IISOle連接通過選擇
//添加引用,選擇COM選項卡,然後找到
//活動DS命名空間提供商
根據我的谷歌搜索:(丟失的鏈接,抱歉)
「活動DS IIS名稱空間提供程序」是IIS安裝的一部分。
安裝IIS後,您會在選項列表中看到該選項。
如果你沒有看到它應該位於C:\ windows \ system32 \ inetsrv \ adsiss.dll。
要安裝IIS: 單擊開始,設置,控制面板,添加或刪除程序,添加或刪除Windows組件,選擇Internet Informatoin Services(IIS)。
大多數我見過的代碼使用的一些組合:使用System.IO
;使用System.DirectoryServices的 ; //右鍵單擊引用,並使用System.Reflection從.NET 添加它; using System.Runtime.InteropServices;使用System.Collections的 ; 使用IISOle; using System.Collections.Specialized;
添加引用時,Active DS名稱空間可能位於COM選項卡下。
我寫了一個基於webmaster-toolkit.com列表的小類。這是爲了避免使用COM和IIS路由或任何IIS引用。
它使用一個包含約400個mimetypes的XML序列化列表,所以通常綽綽有餘,除非你有非常模糊的mimetypes。在這種情況下,你可以添加到XML文件。
完整解決方案can be found here。下面是一個示例:
class Program
{
static void Main(string[] args)
{
var list = MimeType.Load();
MimeType mimetype = list.FirstOrDefault(m => m.Extension == "jpg");
}
}
是的,我。 我將返回所有可能的類型。 我需要允許用戶上傳和下載任何類型的文件 – Shimmy 2009-07-28 03:49:04
正如我所說,沒有「所有」MIME類型的東西。你最好的選擇是獲得一個常見的列表(你可以在這裏將「common」視爲寬或窄),並以二進制形式返回所有內容(「application/octet-stream」)。 – ChssPly76 2009-07-28 03:55:06
我並不是說我所有的意思都是我列舉的常見的 – Shimmy 2009-07-28 04:05:14