【问题标题】:Laravel is not forcing file downloadLaravel 不强制文件下载
【发布时间】:2021-07-31 14:26:34
【问题描述】:

我有一个程序正在生成一个 .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);

【问题讨论】:

    标签: excel laravel file download


    【解决方案1】:

    使用此代码从 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 . '"'
    ]);
    

    【讨论】:

    • 找不到文件...当我使用这段代码时。
    • storage_path中给出你的文件路径
    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2021-11-13
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    相关资源
    最近更新 更多