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

时间:2017-03-29 04:13:24

标签: c# asp.net adobe pdfviewer

我有以下代码,生成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();

图片在这里:sample

想法是暂时生成文件,用户将决定是否保存。我尝试了其他选项,但似乎都没有,特别是如果它在DIV中。换句话说,我希望使用ltEmbed.Text = string.Format(embed, ...);

显示动态PDF文件

3 个答案:

答案 0 :(得分:0)

我们已经在我们的项目中使用过。尝试&amp;让我知道。

DESIGN

 <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)

答案 1 :(得分:0)

我有机会做同样的任务,我在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);

答案 2 :(得分:0)

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

How to stream a generated PDF file to a reader inside a DIV and not the browser