繁体   English   中英

Laravel:无法下载生成的文件

[英]Laravel: Can't download the generated file

我有一个 function 在其中生成 excel (.csv) 文件。 一切正常,除了下载。 所以我的想法是生成csv文件后,在浏览器中自动下载文件。 生成文件后,我们将其存储在公用文件夹中。 So when I call my function, the function is opening the url: http://127.0.0.1:8000/project/tools/export/exportData and displaying the file instead of downloading it. 所以为此我需要你的帮助。 提前谢谢你:我的代码:

public function exportData() {
 
$size=sizeof($arr);
   for($i=0; $i<$size; $i++)
   {
    fputcsv($fh,$arr[$i]);
   }
   fclose($fh);
   $file="Export.csv";
   $headers = array(

    'Content-Type: text/csv',

  );
   return response()->download($file, 'Export.csv', $headers);
  
}

您可以将content-disposition添加到标题中以指示如何处理文件:

$headers = [
    'Content-type'        => 'text/csv',
    'Content-Disposition' => 'attachment; filename="filename.csv"',
];

要在浏览器中显示文件而不是下载它,您可以将attachment替换为inline

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM