11

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??

5 Answers 5

13

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Where do I resolve the path and how do I stream? The message I get now is Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any program.
Yes, @Model.CertificatePath is the actual path and is the correct location. So, I thought I can just open it??
It says invalid characters in path when I typed what you suggested
<a href="file://@Model.CertificatePath" target="_blank" class="button3">Open</a>
What's the value of CertificatePath?
|
6

Try like this in your View

@Html.ActionLink("View", "ViewPDF", new { target = "_blank" })

Now Link will open in new window. You can write the pdf bytes in ViewPDF controller method

Comments

1

You could have the link fire a method such as the one below which will then stream your chosen file to the file download rather than opening the pdf in the broswer.

/// <summary>
/// Forces a file to be displayed to the user for download.
/// </summary>
/// <param name="virtualPath"></param>
/// <param name="fileName"></param>
public static void ForceDownload(string virtualPath, string fileName)
{
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.AddHeader("content-disposition", "attachment; filename=" + fileName);
    response.WriteFile(virtualPath);
    response.ContentType = "";
    response.End();
}

Comments

1

Well if your getting the path value and the value is in @Model.CertificatePath

this wiil not work

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

You will need to add this

<a href="@Url.Content(Model.CertificatePath)" target="_blank" class="button3">Open</a>

and make sure you path is relative by adding this ~ for example if your path is /Content/pdfs/CertificatePath.pdf

it would need to look like

~/Content/pdfs/CertificatePath.pdf

This should be the simplest way to make it work. Hope this helps.

Comments

0

Unfortunately you can't dictate where the PDF will be opened, mainly because you can't guarantee the Adobe Acrobat reader plugin is installed or how it functions.

You could in theory open a new window, and in that new window have a JavaScript function to open the PDF file, but again you can't guarantee it will open in a embedded window without the plugin, the best you can hope for is "best try".

2 Comments

Yes..I know the PDF plug in is there. So I would to go for my best try. How can I do that? My browser says
Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.