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.

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

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答