我正在使用MVC應用程序。我想用Jquery AJAX下載excel文件和PDF文件。如何下載Excel文件和PDF文件使用JQuery Ajax MVC
在瀏覽網頁
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) {
}
});
在控制器
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();
}
所以在控制我們得到的所有數據,但我們不能夠返回到視圖頁面。
你爲什麼需要ajax? – charlietfl 2014-11-03 12:16:59