点击同一按钮发布和下载?

是否可以使用AJAX进行POST,然后在成功时强制下载响应? </ p>

我的代码:</ p>

  $(document).on(“click”,“#CSV”,function(){

var csv_value = $('#result')。table2CSV({
delivery:'value'
});
console.log(csv_value);
$ .ajax({
type:“POST “,
url:”csv.php“,
data:{
csv:csv_value
},
success:function(html){
//下载
}
});

});
</ code> </ pre>

正如您所见,我正在将 HTML </ code>表转换为 CSV </ code>然后 将它发布到我想要生成 .csv </ code>文件的 php </ code>文件中,因为使用JS是不可能的。</ p>

php脚本 。</ p>

  $ file = $ POST ['csv']; 
$ filename = $ file。“
”。date(“Ym-d_H-i”,time( );;
header(“Content-type:application / vnd.ms-excel”);
header(“Content-disposition:csv”.date(“Ymd”)。“。csv”);
header(“Content -disposition:filename =“。$ filename。”。csv“);
$ csv_output = stripcslashes($ _ POST ['csv']);
print $ csv_output;
exit;
</ code> </ pre>

\ n

我可以从我的成功功能开始下载吗?</ p>
</ div>

展开原文

原文

Is it possible to POST using AJAX and then force download with the response when successful?

My code:

$(document).on("click", "#CSV", function () {

    var csv_value = $('#result').table2CSV({
        delivery: 'value'
    });
    console.log(csv_value);
    $.ajax({
        type: "POST",
        url: "csv.php",
        data: {
            csv: csv_value
        },
        success: function (html) {
          //Download
        }
    });

});

As you can see i'm converting my HTML table to CSV then posting it to my php file where I want to generate a .csv file as this is not possible using JS.

php script.

$file = $_POST['csv'];
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
$csv_output=stripcslashes($_POST['csv']);
print $csv_output;
exit;

Can I initiate a download from my success function?

1个回答



我认为最简单的方法是将用户重定向到下载页面,如下所示:</ p>

  success:function(html){
window.location.href =“your / download / path / file.php”;
}
</ code> </ pre>

在此文件中,您准备下载。</ p>
</ div>

展开原文

原文

I think the easiest way would be to redirect the user to the download page, something like this:

success: function (html) {
  window.location.href = "your/download/path/file.php";
}

In this file, you prepare the download.

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