如何将生成的 PDF 文件流式传输到 DIV 内的阅读器,而不是浏览器



我有以下代码,在生成PDF后,向SQL提交查询将显示在浏览器中。但我正在寻找的是将文件显示在页面的 PDF 查看器中,而不是使用浏览器阅读器。查看器位于 DIV 内。您可以看到所附图像中显示的示例,我将其作为测试运行以显示来自本地系统而不是流式传输的文件。此函数 Response.BinaryWrite(bytes(;将生成的 PDF 发送到浏览器查看器。如何让它显示在 DIV 内的查看器中?

string reportLabel = lbReports.Text;
document.Add(table);
document.Close();
byte[] bytes = memoryStream.ToArray();
Response.Clear();
Response.AddHeader("Content-Disposition", "inline");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);

这是应该在 DIV 中显示生成的 PDF 文件的代码:

string embed = "<object data="{0}" type="application/pdf" width="698px" height="450px">";
embed += "If you are unable to view file, you can download from <a href = "{0}">here</a>";
embed += " or download <a target = "_blank" href = "http://get.adobe.com/reader/">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ("blabla.pdf")); /*it shows this PDF as local file just for test*/
memoryStream.Close();
this.Context.ApplicationInstance.CompleteRequest();

此处的图像:示例

这个想法是临时生成文件,用户将决定是否保存它。我尝试了其他选项,但似乎都不起作用,特别是如果它在 DIV 内。换句话说,我希望使用ltEmbed.Text = string.Format(embed, ...);显示动态PDF文件

我们已经在我们的项目中使用了。试着让我知道。

设计

 <div id="pdfPopup" style="display: none;">
    <a href="Javascript:void(0);" id="cl" onclick="document.getElementById('pdfPopup').style.display ='none';"
        class="closebtn2" style="z-index: 1111111;">
        <img src="images/btnClose.png" alt="" />
    </a>
    <div class="overlay" style="visibility: visible !important;">
    </div>
    <div id="popUpContainer" style="border-radius: 8px; -webkit-border-radius: 8px; -moz-border-radius: 8px;
        background-color: #fff; width: 1000px; height: 600px; top: 20px; left: 50%; margin-left: -500px;
        overflow: auto; position: absolute; z-index: 1111111;">
        <object data="" type="application/pdf" width="100%" height="600px">
        </object>
    </div>
 </div>

.JS

 function DisplayPDF() 
 {
        var PDFServerPath = document.getElementById('<%=hdnPdfPath.ClientID %>').value;
        var x = document.getElementsByTagName('object')[0];
        x.setAttribute("data", PDFServerPath);
        document.getElementById('pdfPopup').style.display = "block";
  }

法典

 ScriptManager.RegisterStartupScript(Me, Me.GetType(), "DisplayPDF", "DisplayPDF();", True)

我有机会做同样的任务,我在MVC中做过

我的控制器方法代码如下所示

        byte[] fileContent = Content;
        string base64 = Convert.ToBase64String(fileContent);
        Response.Headers.Remove("Content-Disposition");
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.Headers.Add("Content-Disposition", "inline; filename=MyPdfFile.pdf");
        return File(fileContent, "applicaton/pdf"); 

我的JavaScript是

        var iframe = document.createElement('iframe');
        iframe.frameBorder = 0;
        iframe.width = "890px";
        iframe.height = "550px";
        iframe.id = "frameForMyPdf";
        iframe.setAttribute("src", "/Home/GetPDFContent?PdfId=101");
        document.getElementById("divPdfPreviewPopup").appendChild(iframe);

经过长时间的研究,我被建议了一些解决方法,所以在这里我与解决方案分享这个链接,对我来说它工作得很好。

如何将生成的 PDF 文件流式传输到 DIV 内的阅读器而不是浏览器

最新更新