【问题标题】:How to stream a generated PDF file to a reader inside a DIV and not the browser?如何将生成的 PDF 文件流式传输到 DIV 内的阅读器而不是浏览器?
【发布时间】:2017-03-29 04:13:24
【问题描述】:

我有以下代码,在生成 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 文件

【问题讨论】:

    标签: c# asp.net adobe pdfviewer


    【解决方案1】:

    我们在我们的项目中使用过。尝试并告诉我。

    设计

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-18
          • 2010-12-21
          • 2011-04-21
          • 2015-04-21
          • 2010-10-11
          • 2016-08-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多