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

通过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
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面