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();
}
}