在Tab或iframe中打开动态生成的PDF

时间:2012-03-28 13:29:21

标签: asp.net-mvc pdf

请帮忙。 我显然不是专家,但使用本网站的建议,我认为我真的很接近做以下

能够在中打开动态生成的PDF a)一个新标签 b)iframe

希望我只需要几行正确的语法,我就会在那里。

我使用itextSharp

在控制器中动态生成PDF

CONTROLLER

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

查看

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

为了回应你的建议Darin,我将控制器更改为:

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");
  }

这样做之后,你的建议工作正常,但我意识到我并没有清楚地解释我的意图。因此我改变了VIEW以反映我想要做的事情。

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>

1 个答案:

答案 0 :(得分:6)

动作:

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

要在新标签/窗口中打开:

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

要在iframe中打开您的代码看起来很好。只需确保将Content-Disposition标头设置为内联。