当点击ExportCSV路由时,它会在浏览器上显示所有数据,而不是(下载)导出CSV文件。
控制器:
public function export()
{
$datas = DeviceStorage::all();
$csvFileName = 'min_value.csv';
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $csvFileName . '"',
];
$handle = fopen('php://output', 'w');
fputcsv($handle, ['device_id', 'device_name' , 'min_sale_price']);
foreach ($datas as $data) {
fputcsv($handle, [$data->device_id, $data->Device->device_name, $data->min_sale_price]);
}
fclose($handle);
return Response::make('', 200, $headers);
}
字符串
路线:
Route::get('export/csv' , [DeviceController::class , 'export'])->name('export.csv');
型
刀片文件:
<a class="btn btn-primary" href="{{ url('export/csv') }}">Export CSV</a>
型
1条答案
按热度按时间elcex8rz1#
要解决这个问题,请按照以下步骤操作:
字符串