2

.NET Framework 4.7.2 MVC project

I have a controller action that dynamically loads a PDF to return inline to the browser. Everything is working, except the filename is not showing in the browser title bar or the tab. Instead, the browser is showing the value of the "id" parameter, presumably because it follows the last slash.

I need the browser to display the PDF filename, so that users will know what PDF they are viewing. Our users tend to have multiple PDFs open in tabs and switch between them frequently. This is a simplified version of the code:

public FileContentResult ViewPDF(int id)
{
    byte[] pdfContents = loadPDF(id);
    string filename = getPDFFilename(id);

    var cd = new ContentDisposition
    {
        FileName = filename,
        Inline = true
    };

    Response.AddHeader("Content-Disposition", cd.ToString());

    string returnContentType = MimeMapping.GetMimeMapping(filename);            

    return File(pdfContents, returnContentType);
}

I have also tried a FileStream with no luck and have tried Edge, IE, and Chrome, but they all do the same. In Chrome, when I Save the PDF, it does save with the correct filename but just won't show it in the browser.

Bryan Williams
  • 135
  • 1
  • 11

1 Answers1

0

This isn't the ideal solution, but I ended up creating a new View page that contains a full-screen <iframe> that loads the PDF inline. This way I could at least display the PDF name in the page title, which appears in the tab.

Similar to this:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewData["Title"]</title>
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
        }

        .pdfFrame {
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>

<body class="nomargins">
    <iframe class="pdfFrame" src="@Url.Action("GetPDF", new { id = ViewBag.pdfID })" frameBorder="0"></iframe>
</body>

</html>
Bryan Williams
  • 135
  • 1
  • 11