无法使用 Google Chrome 从 laravel 响应下载简单的 CSV 流
Cannot Download simple CSV stream from laravel response using Google Chrome
当我走 Safari/Firefox 中的 /test-csv
路线时,我可以下载 one-line csv 文件。但是当我在 Chrome 中尝试时,它在控制台的网络选项卡中显示 'Failed to load Response Data'。
*edit*
重要的新细节:它在 Chrome Windows 计算机上确实有效,但在我的 Macbook Pro
上无效
*edit*
我尝试了 Content-Type
和 Content-Disposition
header 的几种变体,但均无济于事。
当我省略所有 header 时,我在浏览器中只得到一个字符串:"id,name,email"
(没有下载)。
Chrome 我需要某种特殊的 header 来强制下载这个流数据吗?
use Symfony\Component\HttpFoundation\StreamedResponse;
Route::get('test-csv', function(){
$response = new StreamedResponse(function(){
// Open output stream
$handle = fopen('php://output', 'w');
fputcsv($handle, [
'id',
'name',
'email'
]);
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment;filename=export.csv;',
]);
return $response;
});
@Bogdan 的评论让我意识到问题一定出在我的 install/settings in Chrome.
我关闭了扩展并卸载并重新安装 chrome。它现在按预期工作。
如果@bogdan 想要 post 他的 comment/suggestion 作为答案,我会将其标记为已接受的答案。
当我走 Safari/Firefox 中的 /test-csv
路线时,我可以下载 one-line csv 文件。但是当我在 Chrome 中尝试时,它在控制台的网络选项卡中显示 'Failed to load Response Data'。
*edit*
重要的新细节:它在 Chrome Windows 计算机上确实有效,但在我的 Macbook Pro
上无效*edit*
我尝试了 Content-Type
和 Content-Disposition
header 的几种变体,但均无济于事。
当我省略所有 header 时,我在浏览器中只得到一个字符串:"id,name,email"
(没有下载)。
Chrome 我需要某种特殊的 header 来强制下载这个流数据吗?
use Symfony\Component\HttpFoundation\StreamedResponse;
Route::get('test-csv', function(){
$response = new StreamedResponse(function(){
// Open output stream
$handle = fopen('php://output', 'w');
fputcsv($handle, [
'id',
'name',
'email'
]);
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment;filename=export.csv;',
]);
return $response;
});
@Bogdan 的评论让我意识到问题一定出在我的 install/settings in Chrome.
我关闭了扩展并卸载并重新安装 chrome。它现在按预期工作。
如果@bogdan 想要 post 他的 comment/suggestion 作为答案,我会将其标记为已接受的答案。