asp.net-mvc 在 Tab 或 iframe 中打开动态生成的 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9908554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open dynamically generated PDF in Tab or iframe
提问by Pete Davies
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
能够在 a) 新标签 b) iframe 中打开动态生成的 PDF
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
我正在使用 itextSharp 在控制器中动态生成 PDF
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:
针对您的建议 Darin,我将 Controller 更改为:
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.
这样做后,您的建议效果很好,但我意识到我没有足够清楚地解释我的意图。因此,我更改了 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>
回答by Darin Dimitrov
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 中打开,您的代码看起来不错。只需确保将 Content-Disposition 标头设置为内联。

