Подтвердить что ты не робот

Asp.net mvc file contenttype

  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, что есть в типе контента. который дает проблемы.

4b9b3361

Ответ 1

Вот список ContentTypes, который вы можете использовать для возврата к просмотру

http://www.wiley.com/legacy/compbooks/graham/html4ed/appb/mimetype.html

Word (.doc)

application/msword

Word (.docx)

application/vnd.openxmlformats-officedocument.wordprocessingml.document

PDF (.pdf)

application/pdf

Ответ 2

Я сделал этот метод некоторое время назад:

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