如何在没有 link 的情况下显示 PDF 文件
How to show PDF file without link
我使用 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);
}
因此,我单击按钮 "Export to PDF" 生成报告,然后显示 link "Download Generated PDF",用户应单击以查看报告。我想在不单击 link 的情况下显示报告,只需单击按钮即可。怎么做?预先感谢您的帮助。
将 iframe 添加到您的 html 并将该 iframe 的 src 属性 设置为 DownloadFile url。
<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
}
}
.
.
.
我使用 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);
}
因此,我单击按钮 "Export to PDF" 生成报告,然后显示 link "Download Generated PDF",用户应单击以查看报告。我想在不单击 link 的情况下显示报告,只需单击按钮即可。怎么做?预先感谢您的帮助。
将 iframe 添加到您的 html 并将该 iframe 的 src 属性 设置为 DownloadFile url。
<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
}
}
.
.
.