I'm generating an Excel file and saving it to server disk, now I need to safely deliver this file to the user. The code below achieve a status code 200
at the browser, but no file is available to download.
How should I do it?
public ActionResult GenerateReport(int customer_id)
{
\\file being created
string fileName = "newReport.xlsx";
string filePath = ConfigurationManager.AppSettings["serverSetting"].ToString() + "\\Content\\reports\\temp\\" + fileName;
excel.SaveAs(new FileInfo(filePath));
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
return File(filePath, contentType, fileName);
}
On the frontend:
$('#btnGenerateReport').on('click', function () {
$.ajax({
url: '/GeneralReport/GenerateReport',
type: "POST",
contentType: "application/json",
data: JSON.stringify({
customer_id: customer_id
}),
success: function () {
$.Notification.autoHideNotify('success', 'top right', 'Success', 'Success');
},
error: function () {
$.Notification.autoHideNotify('error', 'top right', 'Error', 'Error');
}
});
});