I am using below code to create excel and download it in MVC
public ActionResult DownloadExcel()
{
ExcelPackage excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
//data binding to excel here
var stream = new MemoryStream();
excel.SaveAs(stream);
string fileName = "SampleDemoExcel.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, fileName);
}
But above code not working for download.
I am able to save excel in local D: drive using below code
excel.SaveAs(new FileInfo(@"D:\SampleDemoExcel.xlsx"));
But Instead of saving directly in some drive internally, I want download behavior.
How to achieve above scenario
Edit:
Actually, I am calling above method using $.ajax as below
$("#btnDownload").click(function(){
debugger
$.ajax({
url: "/Home/DownloadExcel",
headers: {
'AntiForgeryToken': ''
},
type: "POST",
success: function (data) {
debugger
},
error: function (x, h, r, thrownError) {
//getting errors here as below
// h:parserror
// thrownerror:Error:Invalid XML �M�H�� . . .
}
});
});
I am getting an error in ajax call as parsererror
and Invalid XML �M�H�� ..
How to handle this