asp.net mvc文件contenttype

时间:2010-11-29 13:34:27

标签: c# asp.net-mvc

  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,内容类型中包含的内容。这给了问题。

2 个答案:

答案 0 :(得分:23)

以下是可用于传递回View

的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

答案 1 :(得分: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);