我有pdfExport类,我生成一个pdf。在这个课程中我有一个方法
public MemoryStream returnPDF()
{
using (MemoryStream stream = new MemoryStream())
{
pdfRenderer.PdfDocument.Save(stream, false);
return stream;
}
}
我的控制器如下所示
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Contingency_Report(List<int> ids)
{
pdfExport pdf = new pdfExport(ids);
MemoryStream stream = new MemoryStream();
stream = pdf.returnPDF();
return File(stream.ToArray(), "application/pdf", "contingency.pdf");
}
正如我在Chrome中看到的那样(按下F12键),响应返回pdf(“响应”选项卡),但该文件无法下载。在“响应”选项卡中看到结果之前没有任何反应。如何让它可供下载?我希望浏览器询问保存文件的位置。
答案 0 :(得分:1)
我解决了我的问题。如果有人需要帮助,那就是我做的事情
查看:
$(function () {
$("#btnCont").click(function () {
var idsToSend = [];
var grid = $("#grid_Edit").data("kendoGrid");
var ds = grid.dataSource.view();
for (var i = 0; i < ds.length; i++) {
var row = grid.table.find("tr[data-uid='" + ds[i].uid + "']");
var checkbox = $(row).find(".checkbox");
if (checkbox.is(":checked")) {
idsToSend.push(ds[i].ID);
}
}
// alert(idsToSend);
// $.post("/Contigency/Contingency_Report", { ids: idsToSend});
var url = "";
url = server;
var getUrl = '@Url.Action("Download_Report", "Contigency")';
$.ajax({
url: url + "/Contigency/Contingency_Report",
type: 'post',
contentType: 'application/json',
data: JSON.stringify({
ids: idsToSend
}),
success: function (d) {
if (d.success) {
window.location = getUrl + "?fName="+ d.fName;
}
},
error: function () {
}
})
})
}
);
发布并获取方法
public ActionResult Contingency_Report(List<int> ids)
{
MemoryStream workStream = new MemoryStream();
pdfExport pdf = new pdfExport(ids);
workStream = pdf.returnPDF();
workStream.Position = 0;
var fName = string.Format("Contingency-{0}", DateTime.Now.ToString("s"));
Session[fName] = workStream;
return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet);
}
public ActionResult Download_Report(string fName)
{
var cd = new System.Net.Mime.ContentDisposition
{
FileName = fName+".pdf",
Inline = false,
};
var stream = Session[fName] as MemoryStream;
if (stream == null)
return new EmptyResult();
Session[fName] = null;
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(stream, System.Net.Mime.MediaTypeNames.Application.Pdf);
}
那就是它!