簡體   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