0

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.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • Where's `event` defined within your `fncExtractReport ` function? – Sebastián Palma May 12 '17 at 13:01
  • Sorry, I corrected my code. Its defined upon on click of $('button#btnExtractReport').on('click', function() { fncExtractReport(); }); – Willy David Jr May 12 '17 at 13:04
  • The short answer is that you can't do a post with ajax and expect the browser to open the file. You need to direct the whole browser to the `ExtractReport` action URL. – Niklas May 12 '17 at 13:22
  • Hi @Niklas, I appreciate your help. Can you give sample or even link so that I can have reference on how to implement it? – Willy David Jr May 12 '17 at 13:25
  • @WillyDavidJr You could build the form dynamically and submit it, like in this example: http://stackoverflow.com/a/5524053/536610 – Niklas May 12 '17 at 13:51

1 Answers1

0

you can't get a file from the controller with ajax. you need to do a postback call by submiting the form :

function fncExtractReport() {
            event.preventDefault();
            $('form').submit();
        }

and set the action of the form with :

'@Url.Action("ExtractReport")'
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28