2010-11-29 7 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 file contenttype

ответ

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; 
} 

Вы можете использовать, чтобы установить MimeType, как это:

Response.ContentType = GetMimeType(@CompletePhysicalPath); 
Смежные вопросы