3

I'm building an ASP.NET MVC 4 app that needs to allow the user to display a PDF inline in the browser (not offered for download) in Chrome. I've used the following answers to help me build it thus far:

Here's the action in my controller:

public ActionResult DownloadPdf()
{
    var mimeType = "application/pdf";
    var filepath = Server.MapPath(Url.Content("~/downloads/"));
    var filename = "Sample.pdf";
    Response.Headers.Remove("Content-Disposition");
    Response.AppendHeader("Content-Disposition", "inline; filename=" + filename);
    return File(filepath, mimeType);
}

And here's the JavaScript called to open a new window with the file inline:

function downloadPdf() {
    var path = '@Url.Action("DownloadPdf")';
    window.open(path, '_blank');
}

Using the above code, a new window (tab) opens in Chrome with the exception:

Could not find a part of the path 'C:\_Projects\myapp\PDFHandler\downloads\'.

But the file Sample.pdf is in that folder. I read that to open a file inline, we need to not provide the filename to the File() method. If I change the above to the file path and name, the error goes away but then it offers me the Save dialog again in Chrome.

Alex
  • 29,612
  • 11
  • 60
  • 134

1 Answers1

2

I tested your code locally and it's working as expected, and your PDF file is right as well.

But it's the Browser who has the last word. These are the step to setup PDF previews in chrome:

  1. On your computer, open Chrome.
  2. At the top right, click More More and then Settings.
  3. At the bottom, click Advanced.
  4. Under "Privacy and security," click Content settings.
  5. Near the bottom, click PDF documents.
  6. Turn off Open PDFs using a different application.

Source: https://support.google.com/chrome/answer/6213030?hl=en

hardkoded
  • 13,167
  • 3
  • 34
  • 48