3
請幫忙。 我顯然不是專家,但使用本網站的建議,我覺得我真的很接近做以下在Tab或iframe中打開動態生成的PDF
能夠在 一)新選項卡 B)的iframe打開一個動態生成的PDF
希望我只需要幾行正確的語法,我就會在那裏。
我動態生成使用iTextSharp的
控制器
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>
針對您的建議達林控制器中的PDF,我改變了控制器到:
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>
非常感謝您的回覆。 我真的很希望看到我對原始文章所做的修改之後,您會很樂意幫助完成這些修改。 – 2012-03-28 14:38:10
@PeteDavies,在你的例子中你使用的是AJAX。您不能使用AJAX下載文件。 GetPdf操作不應該是使用AJAX請求的查詢。 – 2012-03-28 14:43:33
哦!在這種情況下,我如何向GetPdf傳遞它所需的信息? – 2012-03-28 14:47:51