Nifle
Nifle

Reputation: 11933

Open pdf in browser plugin

How do I (in my controller) send a pdf that opens in the browser. I have tried this but it only downloads the file (both ie and firefox) without asking.

public ActionResult GetIt()
{
    var filename = @"C:\path\to\pdf\test.pdf";
    // Edit start
    ControllerContext.HttpContext.Response.AddHeader("Content-Disposition", String.Format("inline;filename=\"{0}\"", "test.pdf"));
    // Edit stop
    return File(filename, "application/pdf", Server.HtmlEncode(filename));
}

After adding the edit above it works as it should, thanks.

Upvotes: 5

Views: 3400

Answers (3)

ScottE
ScottE

Reputation: 21630

This (in addition to the other headers) does the trick for me in a plain .net web app:

Response.AddHeader("Content-Disposition", String.Format("inline;filename=""{0}""", FileName))

I'm not familiar with MVC, but hopefully this helps.

Upvotes: 2

Steve Claridge
Steve Claridge

Reputation: 11100

You need to set the Content disposition HTTP header to inline to indicate to the browser that it should try to use a PDF plugin if it is available.

Something like: Content-Disposition: inline; filename=test.pdf

Note that you cannot force the use of the plugin, it is a decision made by the browser.

Upvotes: 6

Tom van Enckevort
Tom van Enckevort

Reputation: 4198

I think this relies on how the client handles PDF files. If it has setup to let Adobe Reader open the files in the browser plugin it will do that, but maybe you have set it up to download the file rather than opening it. In any case, there is no way of controlling how PDF files will be opened on the user's machine.

Upvotes: 1

Related Questions