开发者

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 CompletePhysi开发者_运维百科calPath = FilePathForOrganization.vcr_MetaValue + CvPath +  MetaValue ;
            return File(@CompletePhysicalPath,"");

    }

My File can return doc,docx or pdf , what to have in content type . which is giving problem.


Here's a list of ContentTypes that you can use to pass back to your View

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


I made this method some time ago:

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

You can use that to set the mimetype, like this:

Response.ContentType = GetMimeType(@CompletePhysicalPath);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜