I am trying to create a report and download it using a simple button. The button will create the report on the Controller and return it as a file then should automatically download it. The problem is, the Action is triggered but the file is not downloading. I am not sure what I am missing with my code:
Here is the Javascript part:
$(document).ready(function() {
fncBindExtractReport();
}
function fncBindExtractReport() {
$('button#btnExtractReport').unbind();
$('button#btnExtractReport').on('click', function() {
fncExtractReport();
});
}
function fncExtractReport() {
event.preventDefault();
var objData = $('form').serialize();
$.ajax({
url: '@Url.Action("ExtractReport")',
type: 'POST',
datatype: 'application/JSON',
data: objData,
beforeSend: function() {
},
success: function(result) {
},
error: function(request, status, error) {
}
});
}
Here is the Action and Controller part. I already tried FileResult and even FileContentResult but nothing happens. After the end of the code, nothing happens:
[ActionName("ExtractReport")]
public ActionResult fncExtractReport(entVwModel objModel)
{
LocalReport rptReport = new LocalReport();
string strReportType = "EXCEL";
string strMimeType = string.Empty;
string strEncoding;
string strFileNameExtension = "";
string strDeviceInfo = "<DeviceInfo><SimplePageHeaders>False</SimplePageHeaders></DeviceInfo>";
Warning[] wrnWarnings;
string[] strStreams;
byte[] renderBytes = null;
string strFileName = "Excess 40 Hours Report";
try
{
rptReport.ReportPath = Server.MapPath("~/ReportTemplates/Report.rdlc");
ReportDataSource rdsBSCList = new ReportDataSource("dsReport", dtData.DefaultView.ToTable());
rptReport.DataSources.Add(rdsBSCList);
renderBytes = rptReport.Render(strReportType, strDeviceInfo, out strMimeType, out strEncoding,
out strFileNameExtension, out strStreams, out wrnWarnings);
}
catch (Exception ex)
{
throw ex;
}
if (renderBytes != null)
{
var cd = new System.Net.Mime.ContentDisposition
{
Inline = false, FileName = "test.xls"
};
Response.AppendHeader("Content-Disposition", cd.ToString());
}
return File(renderBytes, strMimeType);
}
This report is working, I already debug it and all datatable are generated properly. I am not sure why there is no downloading happening when clicking my button. There was no error as well on Console on Developer tools.