繁体   English   中英

使用laravel强制下载文件

[英]force download file with laravel

所以我正在使用Laravel 5,我试图强制下载一个选定的文件,但没有任何反应。

public function downloadUserFile(){
        $result = $_POST['filename'];
        $entry = File::where('filename', $result)->firstOrFail();
        $file = Storage::disk()->get($entry->filePath);
        $headers = array('Content-Type' => $entry->mimetype);
        return response()->download(storage_path($entry->filePath), $result, $headers);
}

并且响应头似乎没问题

Accept-Ranges: none
Cache-Control: public
Connection: Keep-Alive
Content-Disposition: attachment; filename="SIGNAL - Nadezdata.mp3"
Content-Length: 4205059
Content-Type: audio/mpeg

你知道什么是错的吗?

我认为问题出在路径上。

默认情况下,在config/filesystems.php本地路径以这种方式定义: storage_path('app') ,您将下载以下路径: storage_path($entry->filePath) (此处不包含app )。

你应该做的是改变:

return response()->download(storage_path($entry->filePath), $result, $headers);

成:

return response()->download(storage_path('app/'.$entry->filePath), $result, $headers);

暂无
暂无

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

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