Download force file from laravel

So, I am using Laravel 5, and I tried to force download the selected file, but nothing happens.

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); } 

and the response headers look fine

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

Do you know what is wrong?

+5
source share
1 answer

I think the problem is on the way.

By default, in config/filesystems.php local path is defined as follows: storage_path('app') and you proceed to load the following path: storage_path($entry->filePath) (here app ).

What you need to do is change:

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

in

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

Source: https://habr.com/ru/post/1239155/


All Articles