如何在新标签页或窗口中显示PDF文件字节

时间:2019-05-15 14:53:30

标签: c# asp.net pdf webforms

我有一种将PDF生成为字节数组的方法,我想将此字节数组(作为PDF)显示在浏览器的新标签中。

这是一段代码

protected void BtnDisplayPdf_OnClick(object sender, EventArgs e)
{
    //...
    byte[] pdf = PdfManager.GeneratePdf(/*some inputs*/);
    //open the above bytes at a new tab as a PDF file
}

该怎么做?我想将其显示为PDF而不实际保存文件!

1 个答案:

答案 0 :(得分:0)

您可以尝试

byte[] bin = File.ReadAllBytes(strPath);
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", bin.Length.ToString());
Response.AddHeader("content-disposition", "attachment;filename=\"GeneratedReport.pdf\"");
Response.OutputStream.Write(bin, 0, bin.Length);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();