我正在尝试生成,然后默认使用php脚本下载csv文件。我有代码在chrome和Internet Explorer中工作正常,但相同的脚本在Firefox中不起作用。在firefox脚本中没有正确生成csv文件。以下是脚本
require('core.php');
$master = new db();
$s = $master->getRecords();
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
download_send_headers(site_title .' '. date("d M Y") . ".csv");
echo array2csv($s);
die();
HTML和JS
<a class="list-group-item" href="javascript:void(0);" onclick="return exportUser();">Export to CSV</a>
function exportUser(){
$.ajax({
url: 'get_csv.php',
type: 'POST',
success: function() {
window.location = 'get_csv.php';
}
});
}
答案 0 :(得分:0)
尝试添加此标头:
header('Content-Type: application/csv; charset=utf-8');
答案 1 :(得分:0)
您需要向js函数添加事件
函数exportUser(event)将执行此操作