简体   繁体   English

Laravel:无法下载生成的文件

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

I have a function where I generates excel (.csv) file.我有一个 function 在其中生成 excel (.csv) 文件。 Everything is working except download.一切正常,除了下载。 So my idea was to download the file automatically in the browser after generating the csv file.所以我的想法是生成csv文件后,在浏览器中自动下载文件。 When the file was generated we store it in the public folder.生成文件后,我们将其存储在公用文件夹中。 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. 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. So for this I need your help.所以为此我需要你的帮助。 Thank you in advance: My codes :提前谢谢你:我的代码:

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);
  
}

You can add the content-disposition to your headers to indicate how to handle file:您可以将content-disposition添加到标题中以指示如何处理文件:

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

To display the file in the browser instead of downloading it, you would exchange attachment for inline .要在浏览器中显示文件而不是下载它,您可以将attachment替换为inline

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

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