【发布时间】:2020-07-21 13:01:02
【问题描述】:
我的实际问题是 ajax 成功后 CSV 文件没有下载 我的ajax方法是
$.ajax({
type: "POST",
url: "<?php echo site_url('reports/csvReports'); ?>",
data: { data: dates, studentPerData: studentPerData },
success: function (data) {},
});
控制器代码是
function csvReports() {
//$studentPerData = $_POST['studentPerData'];
$list = $_REQUEST['data'];
$filename = 'studentreport.csv';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
$output = fopen("php://output", "w");
$header = array_keys($list[0]);
fputcsv($output, $header);
foreach ($list as $row) {
fputcsv($output, $row);
}
fclose($output);
return $output;
}
【问题讨论】:
-
设置标题以强制下载文件..不要返回输出。
-
如何设置header来强制下载文件
标签: javascript ajax codeigniter export-to-csv