laravel和nginx下载文件,我试试这样:
TestController.php
,将变量传递给视图:
public function test()
{
$filepath = '2017/demo.zip';
$filename = 'demo.zip';
return view('download', compact('filepath','filename'));
}
download.blade.php
,接收变量:
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename= {{$filename}}");
header('X-Accel-Redirect: /down/'. {{$filepath}} );
exit;
?>
问题:
1,download.blade.php
的内容是否正确?或者这个文件没有必要?
2,如果需要download.blade.php
,但无法解析{{ }}
中的变量,如何编写它?
答案 0 :(得分:1)
无需渲染视图。你可以使用laravel response helper:
return response()->download(public_path('path_to_directory/demo.zip'));