duan_88598 2016-06-29 01:59
浏览 104
已采纳

下载存储无法正常工作的文件链接 - 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:

  • 写回答

1条回答 默认 最新

  • duanguochi6194 2016-06-29 02:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验