2010-11-29 24 views
16
public ActionResult MyFile(string MetaValue,int OrganizationId=0) 
    {   
      OrganizationUsersDataContext OrgMeta = new OrganizationUsersDataContext(); 
      JobsRepository JobsRespository = new JobsRepository(); 
      string CvPath = JobsRespository.GetCvPath(); 
      var FilePathForOrganization = OrgMeta.OrganizationMetas.FirstOrDefault(m => m.bit_IsDeletable == true && m.int_OrganizationId == OrganizationId && m.vcr_MetaKey == "PhysicalPath"); 
      string CompletePhysicalPath = FilePathForOrganization.vcr_MetaValue + CvPath + MetaValue ; 
      return File(@CompletePhysicalPath,""); 

    } 

我的文件可以返回doc,docx或pdf,內容類型有什麼。這是給問題。asp.net mvc文件內容類型

回答

19

我做了這個方法前段時間:

public static string GetMimeType(string fileName) 
{ 
    string mimeType = "application/unknown"; 
    string ext = Path.GetExtension(fileName).ToLower(); 
    Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); // henter info fra windows registry 
    if (regKey != null && regKey.GetValue("Content Type") != null) 
    { 
     mimeType = regKey.GetValue("Content Type").ToString(); 
    } 
    else if (ext == ".png") // a couple of extra info, due to missing information on the server 
    { 
     mimeType = "image/png"; 
    } 
    else if (ext == ".flv") 
    { 
     mimeType = "video/x-flv"; 
    } 
    return mimeType; 
} 

你可以用它來設置MIME類型,像這樣:

Response.ContentType = GetMimeType(@CompletePhysicalPath);