下载存储无法正常工作的文件链接 - Laravel

下载存储无法正常工作的文件链接 -  Laravel

问题描述:

I am putting my files in storage, and it is working. However, the download link is returning an error.

Here is the view which allows users to upload the files

<div class="form-group{{ $errors->has('notes') ? ' has-error' : '' }}">
       <label class="col-md-4 control-label">Upload your notes</label>
           <div class="col-md-6">
               <input type="file" class="form-control" name="notes">

                   @if ($errors->has('notes'))
                       <span class="help-block">
                               <strong>{{ $errors->first('notes') }}</strong>
                       </span>
                    @endif

           </div>
</div>

Here is my handling of the request in a controller.

    Storage::put(
    'notes/' . $note->id . '.pdf',
     file_get_contents($request->file('notes')->getRealPath())
    );

Everything if firing and the pdf files are being stored in my storage/app/notes directory.

The link I am using to download the files is: /notes/90.pdf, where '90' is the note id.

I have been getting the error

NotFoundHttpException in RouteCollection.php line 161:

You will need to define a route that can serve your file.

Route::get('notes/{id}', function ($id) {
    return response()->download(storage_path('notes/' . $id . 'pdf'));
});

You might want to move the download logic to a controller. And add validations to check if the file exists or not but you get the basic idea.