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:
- https://stackoverflow.com/a/30893427/177416 -- pops open Save dialog
- https://stackoverflow.com/a/3605510/177416 -- same as above
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.