douzhao2047 2015-04-20 13:21
浏览 32
已采纳

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

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

  • duanji9677 2015-04-20 13:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python