第一種:這種獲取MIME類型(Content Type)的方法須要在.NET 4.5以後纔可以支持,可是很是簡單。app
優勢:方便快捷asp.net
缺點:只能在.NET 4.5以後使用spa
public FileResult down() { var file = Server.MapPath(@"\Download\fff.zip"); return File(file, MimeMapping.GetMimeMapping(file)); }
以後能夠直接使用MimeMapping類的靜態方法獲取文件的MIME類型
第二種:若是沒有的MIME TYPE將會返回,自定義方法:
// 自定義一個方法 // 經過系統中的Content Type來獲取MIME TYPE public static string GetMimeType(string fileName) { string mimeType = "application/unknown"; string ext = Path.GetExtension(fileName).ToLower(); RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(ext); if (regKey != null && regKey.GetValue("Content Type") != null) { mimeType = regKey.GetValue("Content Type").ToString(); } return mimeType; }
public FileResult down() { var file=Server.MapPath(@"\Download\ffmpeg.zip"); return File(file, GetMimeType(Path.GetFileName(file))); }