0

I have created an export option (where the user will select a client from a list and press export)

  <li><a href="#" onclick="LoadExport('ExportClient')"><span class="label label-info pull-right">(Download)</span><i class="fa fa-download"></i> Export Client</a></li>

    function LoadReport(reportName) {
            var urlExt;
            switch (reportName) {
                case "ExportClient":
                    urlExt = reportName + "?id=" + $("#clientId").val();

                break;
            }
$.ajax({
            url: '/Exports/' + urlExt,
            contentType: 'application/; charset=utf-8',
            success: function (result) { ?????? },
            error:function(xhr, status) { alert(status)},
            type: 'GET',
            dataType: 'text'

        });
}

[HttpGet]
        public ActionResult ExportClient(string id)
        {
//do stuff
var memoryStream = new MemoryStream();
            var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true);

            using (var csvWriter = new CsvWriter(streamWriter))
            {
                csvWriter.WriteRecords(client);
                // No need to flush as the StreamWriter's Dispose takes care of that.
            }

            memoryStream.Position = 0;

            return File(memoryStream, "text/csv", "clients.csv");
}

So this is executed successfully, but the issue is how do I return the csv on success?

D-W
  • 5,201
  • 14
  • 47
  • 74
  • 1
    You cannot download a file using ajax. You need to redirect to a method that returns the file - refer [Download Excel file via AJAX MVC](https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc) –  Apr 22 '18 at 12:12
  • You can create a Blob and using the download attribute. https://stackoverflow.com/questions/46638343/download-csv-file-as-response-on-ajax-request?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Murat Gündeş Apr 22 '18 at 12:13

0 Answers0