我使用iTextSharp在我的C#代码中创建了一个PDF文件。这是我在视图中的代码:
<span style="float:left;text-align:left;">
<a class="k-button k-button-icontext k-grid-Patient" id="hrefCheckedPatients" href="#" onclick="getChecked();">Export to PDF</a>
<a href="#" id="lnkPdfDownload" style="display:none;" onclick="$(this).hide();">Download Generated PDF</a>
</span>...and then:
$.ajax({
type: "POST",
url: "/PatientReport/ExportToPDF",
dataType: "json",
traditional: true,
data: { uniqueIds: ids },
success: function (data) {
if (data.success) {
$('#lnkPdfDownload').show();
$('#lnkPdfDownload').attr('href', '/PatientReport/DownloadFile' + '?fName=' + data.fName);
} else {
$('#lnkPdfDownload').hide();
}
},
error: function (jqXHR, textStatus, errorThrown) {
$('#checkedMsg').text('@ELSORegistry.Resources.Views.Patient.PatientStrings.CheckedError').show();
$('#hrefCheckedPatients').blur();
}
});
我在控制器中的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ExportToPDF(List<String> uniqueIds) {
//step 1: we create a memory stream that listens to the document
var output = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
//Here is the code for generating PDF...
//The End of ExportToPDF method:
byte[] byteInfo = output.ToArray();
output.Write(byteInfo, 0, byteInfo.Length);
output.Position = 0;
var fName = string.Format("File-{0}.pdf", DateTime.Now.ToString("s"));
Session[fName] = output;
return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet);
}
//And the other method in the controller is here:
public ActionResult DownloadFile(string fName)
{
var ms = Session[fName] as MemoryStream;
if (ms == null)
return new EmptyResult();
Session[fName] = null;
return File(ms, "application/pdf", fName);
}
所以,我生成报告点击按钮&#34;导出到PDF&#34; ,然后我显示一个链接&#34;下载生成的PDF&#34;,用户应该点击查看报告。我想在没有点击链接的情况下显示报告,只需点击按钮即可。怎么做?提前感谢您的帮助。
答案 0 :(得分:3)
向您的html添加iframe,并将该iframe的src属性设置为DownloadFile网址。
<iframe id="myframe" src="" />
.
.
.
success: function (data) {
if (data.success) {
//maybe show the iframe here on this line
$('#myframe').attr('src', '/PatientReport/DownloadFile' + '?fName=' + data.fName);
} else {
//do something else
}
}
.
.
.