通过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.

查看全部
weixin_33739523
weixin_33739523
2016/02/21 20:30
  • codeigniter
  • csv
  • php
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复