本文介绍了在 iframe 中显示 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a solution in place for my site that renders a requested PDF document in a full page using the code below. Now my client wants the document rendered within an iframe. I can't seem to get it to work readily, maybe I am missing something obvious. The first code will properly display the PDF in a new window.

        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 代码如下所示:

The iframe code looks like this:

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

我知道我应该为文件名使用 src 属性,但问题似乎是 iframe 在 Page_Load 事件触发之前加载,因此未创建 PDF.有什么明显的我遗漏了吗?

I know I am supposed to use the src attribute for the filename, but the problem seems to be that the iframe loads before the Page_Load event fires, so the PDF is not created. Is there something obvious I am missing?

推荐答案

使用 ASHX 处理程序.getmypdf.ashx.cs 处理程序的源代码应如下所示:

Use an ASHX handler. The source code to your getmypdf.ashx.cs handler should look something like this:

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 将包含如下内容:

getmypdf.ashx would contain something like this:

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

您的 iframe 将如下所示:

And your iframe would look like this:

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

这篇关于在 iframe 中显示 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 15:07
查看更多