tonini
tonini

Reputation: 13

How to download file from storage app folder in Laravel 8

I want to download a file from Storage/app/public folder.

Somehow it doesn't work with out any error message.

Below is the code I've tried.

web.php

Route::get('/viewAllFile',[DownloadController::class, 'downfunc']);
Route::post('download/{id}',[DownloadController::class, 'download']);

DownloadController.php

class DownloadController extends Controller
{
    public function downfunc()
    {
        $downloads = DB::table('files')->get();
        return view('download.viewfile', compact('downloads'));
    }

    public function download($file_name)
    {
        //Storage::disk('local')->put('example.txt', 'Contents');
        $file = Storage::disk('public')->get($file_name);
        $filepath = storage_path("app/{$data->file}");
        
        return \Response::download($filepath);
    }
}

viewfile.blade.php

@foreach($downloads as $down)
    <tr>
        <td>{{$down -> name}}</td>
        <td>{{$down -> directory}}</td>
        <td>{{$down -> updated_at}}</td>
        <td>
            <a href="{{url('/download/'.$down->name)}}" download="{{$down-> name}}">
                <button type="button" class="btn btn-primary">Download</button>
            </a>
        </td>
    </tr>
@endforeach

Upvotes: 1

Views: 11663

Answers (1)

Davit Papalashvili
Davit Papalashvili

Reputation: 41

First, you must run this command:

php artisan storage:link

For example:

Route::get('/download', function (){
    return Storage::download('public/file_name.png');
});

You can find documentation here

Upvotes: 3

Related Questions