Reputation: 709
I am using MVC Application. I want to download excel file and PDF file using Jquery AJAX.
In View page
<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) {
}
});
In 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();
}
So in controller we are getting the all data but we are not able return into view page.
Upvotes: 3
Views: 21622
Reputation: 474
You can try this Solution. In View Page
@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" })
if you want to Change the value of parameter1 and parameter2 dynamically than you can use javascript as describe below
In 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);
});
In 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();
}
Upvotes: 3
Reputation: 539
I would suggest you to make things a bit more simply with help of tools which are coming out of the box. The System.Web.MVC.Controller.File provides you a method which will do exactly what you need using byte array or stream or file path. So instead of this part (and same for pdf)
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();
I would use something like this
File(excelStream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Octet, executeRepType + ".xlsx");
And there is no actual need in async request. So you can just use direct link.
Upvotes: 0