File display inline throws 'Could not find part of path'

I am creating an ASP.NET MVC 4 application that should allow the user to display a PDF in a browser (not offered for download) in Chrome. I've used the following answers to help me build it so 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 where JavaScript is called to open a new window with an embedded file:

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 part of path 'C: \ _ Projects \ myapp \ PDFHandler \ downloads \'.

But the Sample.pdf file is in this folder. I read that in order to open an embedded file, we don't need to provide a filename to the method File()

. If I change the above file path and name, the error goes away, but then it prompts me again for the Save dialog in Chrome.

+3


source to share


1 answer


I checked your code locally and it works as expected and your PDF is correct as well.

But this is the browser that has the final say. This is the step to configure PDF viewer in chrome:



  • On your computer, open Chrome.
  • In the upper right corner, click "Advanced" and then "Settings".
  • At the bottom, click "Advanced".
  • In the Privacy & Security section, click Content Options.
  • At the bottom, click PDF Documents.
  • Disable Open PDF files using another application.

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

+2


source







All Articles