ASP.NET MVC open pdf file in new window

asp.net-mvc, pdf

Solution

You will need to provide a path to an action that will receive a filename, resolve the full path, and then stream the file on disk from the server to the client. Clients out in the web, thankfully, cannot read files directly off your server's file system (unless... are you suggesting `@Model.CertificatePath` is the path to the file on the remote user's machine?).

public ActionResult Download(string fileName)
{
   string path = Path.Combine(@"C:\path\to\files", fileName);

   return File(path, "application/pdf");

}

Update

If `@Model.CertificatePath` is the location on the client's actual machine, try:

 <a href="file://@Model.CertificatePath" target="_blank" class="button3">Open</a>

Note that some browsers may have security settings disallowing you from opening local files.

Problem

I have a MVC application. I need to open the pdf file when user clicks the open button on the page. The filepath where the pdf is stored is read from the database and it is a file on c:. How do I open it in my html code? I have this code: ``` <a href="@Model.CertificatePath" target="_blank" class="button3">Open</a> ``` but this doesn't open my file. What do I have to do? I need to specify somewhere that it is a pdf??

Original source