微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel 不强制下载文件

如何解决Laravel 不强制下载文件

我有一个功能,程序正在生成一个 .csv 文件,然后我想在浏览器中下载。问题是:调用函数时,文件显示在浏览器中,而不是下载它。有什么建议吗?

$csv=array($index,$value);
            
$filename='persons.xls';
$handle=fopen('persons.xls','w');
   
$headers = ['Content-Type: text/xls'];
foreach ($csv as $row) {
    fputcsv($handle,$row);
}
fclose($handle);
return Response::download(public_path('/persons.xls'),'persons.xls',$headers);

解决方法

使用此代码从 Laravel 下载 Microsoft Excel 文件:

$filename='persons.xls';
//---Get path from storage directory
//$path = storage_path('app/' . $filename);
//---Get the path from your public directory
$path = public_path('/persons.xls');

// Download file with custom headers
return response()->download($path,$filename,[
    'Content-Type' => 'application/vnd.ms-excel','Content-Disposition' => 'inline; filename="' . $filename . '"'
]);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。