我们希望在浏览器中显示PDF而不是下载它。在版本为11.15.16299
及更高版本的IE11中,这可以按预期工作。在IE11 11.0.9600
及更低版本中,PDF将作为下载输出而不是显示。控制台内没有错误。是否有< = IE11的解决方案?
*注意:由于此处所述的原因,我们无法保存文件,pdf-base64不是一个选项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();
}
}