加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在iframe中显示PDF

发布时间:2020-12-16 00:10:23 所属栏目:asp.Net 来源:网络整理
导读:我为我的网站提供了一个解决方案,使用下面的代码在整页中呈现所请求的PDF文档.现在我的客户想要在iframe中呈现文档.我似乎无法让它轻松工作,也许我错过了一些明显的东西.第一个代码将在新窗口中正确显示PDF. if (File.Exists(filename)) { //Set the appropr
我为我的网站提供了一个解决方案,使用下面的代码在整页中呈现所请求的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.有什么明显的东西我不见了吗?

解决方法

使用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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读