2017-03-29 27 views
0

我有下面的代碼,生成PDF後提交查詢到SQL將顯示在瀏覽器中。但我期待的是在頁面中的PDF查看器中顯示文件,而不是使用瀏覽器閱讀器。查看器在DIV中。您可以看到附加映像中顯示的示例,我將其作爲測試運行,以顯示本地系統中的文件,而不是流式傳輸。這個函數Response.BinaryWrite(bytes);將生成的PDF發送到瀏覽器查看器。我該怎麼辦才能在DIV內的查看器中顯示呢?如何將生成的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, ...);

回答

0

顯示動態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) 
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); 
相關問題