当用户点击按钮时,它将下载csv文件。它适用于IE9和Chrome,但不适用于Firefox。在Firefox 20中,未设置内容类型,因此将其下载为Firefox HTML文档。
$filename = 'exportedLogs.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=" .$filename);
header("Expires: 0");
header("Pragma: public");
$csv_file = fopen('php://output', 'w');
$header_row = array('id', 'project', 'customer', 'time spent');
fputcsv($csv_file, $header_row);
foreach ($logs as $log) {
$log = array(
$log['Log']['id'],
$log['Log']['project_id'],
$log['Log']['customer_id'],
$log['Log']['time_spent']
);
fputcsv($csv_file, $log);
}
fclose($csv_file);
答案 0 :(得分:0)
我发现标题没有改变的原因是cakePhp。对于遇到此问题的其他人,您必须做两件事:
@ThaJeztah提供了一个可能对其他人有帮助的链接。它适用于json / xml,但主体也可用于csv。
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#json-and-xml-views