0

Scenario:

I have a datatable, the user filters / sort the table and click the export button. Since the datatable is built on server side processing, I had to write the logic for export seperately. When I click the export, the file is not downloading.

Code

[HttpPost]
public FileResult ExportExcelGrid(DataTableAjaxPostModel model)
{
    var objFinderModel = _specFinderExportFlow.ConvertToExportable(model);
    string dateTime = DateTime.Now.ToString("ddMMyyyy");
    var fileDownloadName = "Spec_Finder_Results_" + dateTime + ".xlsx";
    return File(objFinderModel, ExcelExportHelper.ExcelContentType,fileDownloadName);
}

Javascript:

$("#lsc-finder-productexport").on("click", function () {
    $.ajax({
        type: "POST",
        url: "@Url.Action("ExportExcelGrid", "SpecFinder")",
        data: findertable.ajax.params(),
        success: function (response, status, request) {
        },
        failure: function (response) {
        },
        error: function (response) {
        }
    });
});

The call is getting executed, the binary data is returned. How can I download as a file?

This is what I'm getting

enter image description here

Update #2:

I have changed a code like first it'll download the excel file in server and at the ajax success, I'll call the path of the file. which still doesn't work.

Code:

$("#lsc-finder-productexport").on("click", function () {
    $.ajax({
        type: "POST",
        url: "@Url.Action("ExportExcelGrid", "SpecFinder")",
        data: findertable.ajax.params(),
        success: function (response, status, request) {
            window.location.href = response;
        },
        failure: function (response) {
        },
        error: function (response) {
        }
    });
});

C#:

[HttpPost]
public string ExportExcelGrid(DataTableAjaxPostModel model)
{
    var objFinderModel = _specFinderExportFlow.ConvertToExportable(model);
    string dateTime = DateTime.Now.ToString("ddMMyyyy");
    var fileDownloadName = Guid.NewGuid().ToString() + "_Spec_Finder_Results_" + dateTime + ".xlsx";
    var webHost = System.Web.HttpContext.Current.Request.Url.Host;
    var webPort = System.Web.HttpContext.Current.Request.Url.Port;
    var webUrl = webHost + ":" + webPort + "/Content/Export/" + fileDownloadName;
    var serverfilename = Server.MapPath("~/Content/Export/") + fileDownloadName;
    System.IO.File.WriteAllBytes(serverfilename, objFinderModel);
    return webUrl;
}

The file is generated in that Export folder, but its not downloading, the download gets cancelled.

enter image description here

Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76
  • 1
    You can't download a file via ajax, you're going to have to redirect to another page that serves the download or just make a full form submit – JohanP Jul 19 '18 at 02:50
  • See if this answers your question https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request – Sree Harsha Nellore Jul 19 '18 at 02:52
  • You can't return `FileResult` from AJAX callback. Following PRG pattern, you should put `window.location.href` after successful callback with URL points to `GET` action method which returns `FileResult`. – Tetsuya Yamamoto Jul 19 '18 at 02:53
  • But I need to pass datatable's ajax parameter, how can I do that on window.location.href? It also must be a post call? – Ranjith Varatharajan Jul 19 '18 at 02:54
  • You should do something like this inside `success` part of AJAX response: `window.location.href = '@Url.Action("Download", "ControllerName")'`. Then create a `HttpGet` controller with all necessary arguments and return `FileResult` from there. – Tetsuya Yamamoto Jul 19 '18 at 02:57

1 Answers1

0

you need to create link for download so it download automatically and you have to give file name hope this one help you

$("#lsc-finder-productexport").on("click", function () {
    $.ajax({
        type: "POST",
        url: "@Url.Action("ExportExcelGrid", "SpecFinder")",
        data: findertable.ajax.params(),
        success: function (response, status, request) {
            var dowloadlink = document.createElement("a");
                    dowloadlink.download = "test";
                    dowloadlink.href = response;
                    dowloadlink.click();
        },
        failure: function (response) {
        },
        error: function (response) {
        }
    });
});
Mustufa
  • 116
  • 4