lawkunchi's avatar

Get download link of a file in Laravel 5.7

How to generate a download link of a file uploaded by a user.

0 likes
5 replies
Guru5005's avatar

It depends on where you are storing the file, like example if you are storing the file in

public/uploads

than following will work if u are fetching it from database (single row)

<a href="/uploads/{{ $data->value }}"> Download </a>
lawkunchi's avatar

My files are being stored in the Storage folder. When I use this method .

I get this error 'Sorry, the page you are looking for could not be found.

signar's avatar
signar
Best Answer
Level 27

@lawkunchi you can use the download response like so:

return response()->download($pathToFile);
1 like
lawkunchi's avatar

I wanted to download a file uploaded by user and it was saved in the user's table. Below is my final code. Thank you!

 public function download($id) {
        $user = User::findorFail($id);
        return Storage::download($user->file);
    }
Route::get('download/{id}', [
            'uses' => 'UserController@download',
            'as' => 'file.download'
        ]);
1 like

Please or to participate in this conversation.