asp.net mvc file contenttype

Loading...

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

    }

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

Solutions/Answers:

Answer 1:

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

Answer 2:

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

Our Awesome Tools

References

Loading...