在标签或iframe打开动态生成的PDF [英] Open dynamically generated PDF in Tab or iframe

查看:579
本文介绍了在标签或iframe打开动态生成的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。
我显然不是专家,但使用本网站的建议,我觉得我真的很接近执行以下操作

能在打开一个动态生成PDF
a)新标签
B)一个iframe

我希望,我只需要一对夫妇的正确语法行,我会在那里。

我使用iTextSharp的动态生成的PDF在控制器

控制器

 公共FileStreamResult GetPdf()
  {
    ...
    返回新FileStreamResult(Response.OutputStream,应用程序/ PDF格式){FileDownloadName =download.pdf};
  }

查看

 <输入ID =btnN​​ewTab类型=按钮值=新标签/>
<输入ID =btnIframe类型=按钮值=的iFrame/>< D​​IV ID =pdfDiv>< / DIV><脚本类型=文/ JavaScript的>
  $(函数(){    $(#btnIframe)。点击(函数(){
      $获得('/ PDFTest / GetPdf',函数(pdf){
        警报(pdf.length); //按预期工作
        //什么我需要把这里获取PDF出现在IFRAME
      });
    });    $(#btnNewTab)。点击(函数(){
      //问我,如果我想打开或保存PDF。
      //如果我选择打开的PDF在一个完全新窗口中打开。
      //取而代之的是对话,我只想PDF在新标签中打开
      //我可能在完全错误的方式去这件事。
      VAR HTML =< IFRAME SRC =/ PDFTest / GetPdf风格=宽度:100%;高度:600px的'>< / IFRAME>中;
      $('#pdfDiv')的html(HTML)。
    });  });
< / SCRIPT>

在响应您的建议达林,我改变了控制器为:

 公共FileStreamResult GetPdf(someInfo从View)
  {
    ...
      Response.ContentType =应用程序/ PDF
      Response.AddHeader(内容处置,内联;的test.pdf);
      将Response.Buffer =真;
      Response.Clear();
      Response.OutputStream.Write(ms.GetBuffer(),0,ms.GetBuffer()长。);
      Response.OutputStream.Flush();
      到Response.End();      返回新FileStreamResult(Response.OutputStream,应用程序/ PDF格式);
  }

已经这样做了,你的建议,工作得很好,但我知道,我没有解释我的意图不够清楚。因此,我已经改变了VIEW,以反映什么,我试图做的。

 输入ID =btnN​​ewTab类型=按钮值=新标签/>
<输入ID =btnIframe类型=按钮值=的iFrame/>
< IFRAME ID =iframe中>< / IFRAME>
< D​​IV ID =pdfDiv>
< / DIV>
<脚本类型=文/ JavaScript的>
  $(函数(){    $(#btnIframe)。点击(函数(){      $阿贾克斯({
        网址:'/ PDFTest / GetPdf',
        键入:GET,
        数据:JSON,//这条线将不会是一个问题
        数据类型:JSON
        的contentType:应用程序/ PDF,//此行可能是一个问题
        成功:函数(pdf){
          //什么,我需要需要做的,显示在上面iframe中PDF
          $(#IFRAME),ATTR(src用户,PDF格式)。 // HTTP错误400 - 错误的请求。
        }
      });
    });    $(#btnNewTab)。点击(函数(){
      $阿贾克斯({
        网址:'/ PDFTest / GetPdf',
        键入:GET,
        数据:JSON,//这条线将不会是一个问题
        数据类型:JSON
        的contentType:应用程序/ PDF,//此行可能是一个问题
        成功:函数(pdf){
          //什么,我需要需要做的DISPLY PDF在新窗口
        }
      });
    });  });
< / SCRIPT>


解决方案

动作:

 公众的ActionResult GetPdf()
{
    字节[] = PDF ...
    Response.AppendHeader(内容处置,内联;的test.pdf);
    返回文件(PDF,应用程序/ PDF格式);
}

要在新标签/窗口打开:

  @ Html.ActionLink(查看PDF,getpdf,somecontroller,空,新的目标{=_blank})

要在iframe的code看起来很好开。只要确保内容处置头设置为内联。

Please help. I am obviously no expert but using suggestions from this site, I think I am really close to doing the following

Be able to open a dynamically generated PDF in a) a new Tab b) an iframe

Hopefully, I just need a couple of lines of the correct syntax and I will be there.

I am dynamically generating the PDF in a controller using itextSharp

CONTROLLER

public FileStreamResult GetPdf()
  {
    ...
    return new FileStreamResult(Response.OutputStream, "application/pdf"){FileDownloadName = "download.pdf"};
  }

VIEW

<input id="btnNewTab" type="button" value="New Tab" />
<input id="btnIframe" type="button" value="Iframe" />

<div id="pdfDiv"></div>

<script type="text/javascript">
  $(function () {

    $("#btnIframe").click(function () {
      $.get('/PDFTest/GetPdf', function (pdf) {
        alert(pdf.length);  // Works as expected
        // What do I need to put here to get the pdf to appear in a iframe
      });
    });

    $("#btnNewTab").click(function () {
      // asks me if I want to Open or Save the PDF.
      // If I choose Open, the PDF opens in a completely new Window.
      // Instead of the dialog, I just want the PDF to open in a new Tab
      // I am probably going about this in completely the wrong way.
      var HTML = "<iframe src='/PDFTest/GetPdf' style='width: 100%; height: 600px' ></iframe>";
      $('#pdfDiv').html(HTML);
    });

  });
</script>

In response to your suggestion Darin, I changed the Controller to:

public FileStreamResult GetPdf(someInfo from View)
  {
    ...
      Response.ContentType = "application/pdf";
      Response.AddHeader("Content-Disposition", "inline;test.pdf"); 
      Response.Buffer = true;
      Response.Clear();
      Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
      Response.OutputStream.Flush();
      Response.End();

      return new FileStreamResult(Response.OutputStream, "application/pdf");
  }

Having done that, your suggestions worked fine but I realise that I did not explain my intentions clearly enough. I have therefore changed the VIEW to reflect what i am trying to do.

input id="btnNewTab" type="button" value="New Tab" />
<input id="btnIframe" type="button" value="Iframe" />
<iframe id="iframe"></iframe>
<div id="pdfDiv">
</div>
<script type="text/javascript">
  $(function () {

    $("#btnIframe").click(function () {

      $.ajax({
        url: '/PDFTest/GetPdf',
        type: "GET",
        data: json,  // This line will not be a problem
        dataType: "json",
        contentType: "application/pdf", // This line might be a problem
        success: function (pdf) {
          // What to I need to need to do to display the PDF in the above iframe
          $("#iframe").attr("src", pdf); // HTTP Error 400 - Bad Request.
        }
      });
    });

    $("#btnNewTab").click(function () {
      $.ajax({
        url: '/PDFTest/GetPdf',
        type: "GET",
        data: json,  // This line will not be a problem
        dataType: "json",
        contentType: "application/pdf", // This line might be a problem
        success: function (pdf) {
          // What to I need to need to do to disply the PDF in a new window
        }
      });
    });

  });
</script>

解决方案

Action:

public ActionResult GetPdf()
{
    byte[] pdf = ...
    Response.AppendHeader("Content-Disposition", "inline;test.pdf");
    return File(pdf, "application/pdf");
}

To open in new Tab/Window:

@Html.ActionLink("view pdf", "getpdf", "somecontroller", null, new { target = "_blank" })

To open in an iframe your code looks fine. Just make sure to set the Content-Disposition header to inline.

这篇关于在标签或iframe打开动态生成的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆