2012-07-22 354 views
2

我有一个适合我的网站的解决方案,它使用下面的代码在完整页面中呈现请求的PDF文档。现在我的客户想要在iframe中呈现文档。我似乎无法让它工作,也许我错过了一些明显的东西。第一个代码将在新窗口中正确显示PDF。在iframe中显示PDF

 if (File.Exists(filename)) 
     {    
      //Set the appropriate ContentType. 
      Response.ContentType = "Application/pdf"; 
      Response.WriteFile(filename); 
      Response.End(); 
     } 
     else Response.Write("Error - can not find report"); 

iframe的代码如下所示:

<iframe runat="server" id="iframepdf" height="600" width="800" > </iframe> 

我知道我应该使用的文件名src属性,但问题似乎是Page_Load事件触发之前的iframe负荷,所以PDF不会被创建。有什么明显的我失踪?

回答

7

使用ASHX处理程序。源代码到你的getmypdf.ashx.cs处理程序应该是这个样子:

using System; 
using System.Web; 

public class getmypdf : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     Response.ContentType = "Application/pdf"; 
     Response.WriteFile("myfile.pdf"); 
     Response.End(); 
    } 

    public bool IsReusable 
    { 
    get 
    { 
     return false; 
    } 
    } 
} 

getmypdf.ashx将包含这样的事情:

<% @ webhandler language="C#" class="getmypdf" %> 

和你的iframe应该是这样的:

<iframe runat="server" id="iframepdf" height="600" width="800" src="..../getmypdf.ashx"> </iframe> 
0

您需要将src=""设置为提供PDF的其他URL。

0

你尝试:

window.onload = function() { 
    document.getElementById("iframepdf").src = "the URL that loads the PDF" 
} 
1

我真的解决了这个问题!解决方案是使用呈现PDF的代码(它的肉是Response ...代码)生成另一个aspx页面(showpdf.aspx),然后在iframe中调用该代码。我从源页面传递必要的变量。谢谢大家!

<iframe runat="server" id="iframepdf" height="600" width="800" > 
</iframe> 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      String encounterID = Request.QueryString["EncounterID"]; 
      iframepdf.Attributes.Add("src", "showpdf.aspx?EncounterID=" + Request.QueryString["EncounterID"]); 
     }