我正在使用 MVC 应用程序。我想使用 Jquery AJAX 下载 Excel 文件和 PDF 文件。
在查看页面
<a href="javascript:void(0)" class="excelbtn" data-is-pdf="false" >Export To Excel</a>
<a href="javascript:void(0)" class="pdfbtn" data-is-pdf="true">Export To PDF</a>
Jquery ajax
$.ajax({
type: 'GET',
url: '/Report/ExportReports',
contentType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
data: {
Parameter1: Parameter1,
Parameter2: Parameter2,
},
cache: false,
success: function (isSuccess) {
if (isSuccess.Success) {
}
} else {
alert('Something went wrong. Please try again after sometime...');
}
},
error: function (data, status, e) {
}
});
在 Controller 中
public ActionResult ExportReports(string Parameter1, string Parameter2)
{
if (Parameter1 = "PDF")
{
DataTable exportData = grid.GetExportData(dataSource);
MemoryStream pdfStream = gridData.ExportToPDF(exportData, repType);
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + executeRepType + ".pdf");
Response.BinaryWrite(pdfStream.ToArray());
Response.End();
}
else
{
DataTable exportData = grid.GetExportData(dataSource);
MemoryStream excelStream = gridData.ExportToExcel(exportData, executeRepType);
//Write it back to the client
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + executeRepType + ".xlsx");
Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray());
Response.End();
}
return View();
}
因此,在 Controller 中,我们正在获取所有数据,但无法返回到 View 页面。
最佳答案
您可以尝试这个解决方案。 在查看页面
@Html.ActionLink("Export To Excel", "ExportReports", new { isPdfExport = false,Parameter1="_Parameter1" ,Parameter2="_Parameter2"}, new { @class="excelbtn" })
@Html.ActionLink("Export To PDF", "ExportReports", new { isPdfExport = true,Parameter1="_Parameter1" ,Parameter2="_Parameter2"}, new { @class="pdfbtn" })
如果你想动态更改parameter1和parameter2的值,你可以使用javascript,如下所述
在 JavaScript 中:-
$('.excelbtn').attr('href', function () {
return this.href.replace('_Parameter1', Value1).replace('_Parameter2',Value2);
});
$('.pdfbtn').attr('href', function () {
return this.href.replace('_Parameter1', Value1).replace('_Parameter2',Value2);
});
在 Controller 中:-
public ActionResult ExportReports(bool isPdfExport,string Parameter1, string Parameter2)
{
if (Parameter1 = "PDF")
{
DataTable exportData = grid.GetExportData(dataSource);
MemoryStream pdfStream = gridData.ExportToPDF(exportData, repType);
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + executeRepType + ".pdf");
Response.BinaryWrite(pdfStream.ToArray());
Response.End();
}
else
{
DataTable exportData = grid.GetExportData(dataSource);
MemoryStream excelStream = gridData.ExportToExcel(exportData, executeRepType);
//Write it back to the client
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + executeRepType + ".xlsx");
Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray());
Response.End();
}
return View();
}
关于jquery - 如何使用 Jquery Ajax MVC 下载 Excel 文件和 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26713859/