通过AJAX发布下载CSV


                    

您好,Stackoverflow。 我正在尝试通过AJAX调用PHP脚本,该脚本将创建并下载CSV文件。 我知道通常不应该这样做,但是我想这样做。</ p>

下面的我的ajax返回以下结果所示的csv数据:</ p>

”在此处输入图片描述“ </ p>

  $。ajax({
     输入:“ POST”,
     网址:“ <?= site_url('front_office / get_csv /')?>”,
     数据:{hashed_center_ids:hashed_center_ids,print_data:print_data},
     dataType:“文本”,
     成功:功能(响应){
         console.log(响应)
         var uri ='data:application / csv; charset = UTF-8,'+ encodeURIComponent(response);
         window.open(uri,'test.csv');
     }
});
</ code> </ pre>

问题是使用线条</ p>

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

不以CSV格式下载文件。 但是,它没有给它扩展名“ download”。 有人知道我该怎么做,以便使用.csv扩展名下载吗? 谢谢。</ p>
     </ div>

展开原文

原文

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个回答


我无法评论,因此:您可以通过在标头中添加以下内容来更改csv的名称</ p>

  filename = whatever.csv
</ code> </ pre>
     </ div>

展开原文

原文

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

 filename=whatever.csv

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐