0

We would like to display a PDF in the browser instead of downloading it. In IE11 with version 11.15.16299 and above this works as expected. In IE11 11.0.9600 and below, the PDF is output as a download instead of displaying it. There are no errors within the console. Is there a solution for <=IE11?

*Note: we cannot save the file and pdf-base64 is not an option for the reasons described here Recommended way to embed PDF in HTML?

Html

<!-- Tried workaround foo.pdf from https://stackoverflow.com/a/8882132/7302498 -->
<form action="MyWebApiUrl/WriteFileStreamToResponse/foo.pdf" method="post">
   <input type="hidden" name="transferdata" id="transferdata"  />
   <input type="submit" value="Send" />
</form>

Web-Api (C#)

private void WriteFileStreamToResponse(transferdata)
{
    var aFileStream= GetPdfAsFilestream(transferdata);
    byte[] pdfBytes = ReadFully(aFileStream);
    var response = HttpContext.Response;
    response.ClearContent();
    response.ContentType = "application/pdf";
    response.AddHeader("Content-Disposition", "inline");
    response.AddHeader("Content-Length", pdfBytes.Length.ToString());
    response.BinaryWrite(pdfBytes);
    response.End();
}

private static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}
Astrophage
  • 1,231
  • 1
  • 12
  • 38
  • Check this please: https://stackoverflow.com/questions/9195304/how-to-use-content-disposition-for-force-a-file-to-download-to-the-hard-drive – Oguz Ozgul Oct 25 '17 at 11:15
  • thanks but this is for downloading a PDF. But we want to display it in the IE <= Version 11 – Astrophage Oct 25 '17 at 11:18
  • Content-Disposition header is documented on ietf.org: https://tools.ietf.org/html/rfc6266, and says: On the other hand, if it matches "inline" (case-insensitively), this implies default processing. Therefore, the disposition type "inline" is only useful when it is augmented with additional parameters, such as the filename (see below). – Oguz Ozgul Oct 25 '17 at 11:18
  • Thank you. I tried it now with `inline;filename=foo.pdf`, `attachment;filename=foo.pdf`, `attachment`. It is unfortunately still always output as download. – Astrophage Oct 25 '17 at 11:39
  • 2
    Is Adobe PDF Reader add-on enabled? https://helpx.adobe.com/acrobat/using/display-pdf-in-browser.html – Oguz Ozgul Oct 25 '17 at 11:54
  • 1
    Which makes it a client-dependent behaviour for good. – Oguz Ozgul Oct 25 '17 at 11:55

0 Answers0