使用laravel强制下载文件

时间:2015-12-24 19:11:41

标签: php laravel download laravel-5 laravel-response

所以我正在使用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

你知道什么是错的吗?

1 个答案:

答案 0 :(得分:1)

我认为问题出在路径上。

默认情况下,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);