尝试以下代码:
$headers = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename=list.csv',
'Expires' => '0',
'Pragma' => 'public'
];
$list = List::all()->toArray();
# add headers for each column in the CSV download
array_unshift($list, array_keys($list[0]));
$FH = fopen(storage_path('list.csv'),'w');
foreach ($list as $row) {
fputcsv($FH, $row);
}
fclose($FH);
$path = storage_path('list.csv');
return response()->download($path, 'list.csv', $headers);
以下将放入检查元素网络调用
我在堆栈溢出中遇到了同样的问题,但对我却不起作用。 Exporting CSV response laravel 5.5 and download as csv file
对此有什么想法吗?