weixin_33739523 2016-02-21 20:30 采纳率: 0%
浏览 54

通过AJAX发布下载CSV

Hi there Stackoverflow. I am trying to call a PHP script via AJAX that will create and download a CSV file. I know this isn't normally supposed to be done, however I would like to do it this way.

My ajax below returns the csv data as shown by this output:

enter image description here

$.ajax({
    type: "POST",
    url: "<?=site_url('front_office/get_csv/')?>",
    data: {hashed_center_ids : hashed_center_ids, print_data : print_data},
    dataType: "text",
    success: function(response) {
        console.log(response)
        var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(response);
        window.open(uri, 'test.csv');
    }
});

The problem is that using the lines

var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(response); window.open(uri, 'test.csv');

does not download the file as a CSV. But rather it give it no extension with the name "download". Does anybody know how can I make it so it downloads with the .csv extension? Thanks.

  • 写回答

1条回答 默认 最新

  • 叼花硬汉 2016-02-21 21:23
    关注

    I can't comment, so: You can change the name of the csv by adding following to the header

     filename=whatever.csv
    
    评论

报告相同问题?