CSVHelper在ajax调用上导出CSV

时间:2018-04-22 11:43:37

标签: jquery asp.net-mvc csvhelper

我创建了一个导出选项(用户将从列表中选择一个客户端并按导出)

  <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");
}

所以这是成功执行的,但问题是如何在成功时返回csv?

0 个答案:

没有答案