【问题标题】:Download link for files in storage not working - Laravel存储中文件的下载链接不起作用 - Laravel
【发布时间】:2016-10-31 13:00:08
【问题描述】:

我正在将我的文件放入存储中,它正在工作。但是,下载链接返回错误。

这是允许用户上传文件的视图

<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>

这是我在控制器中对请求的处理。

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

所有触发和 pdf 文件都存储在我的 storage/app/notes 目录中。

我用来下载文件的链接是:/notes/90.pdf,其中“90”是笔记 ID。

我遇到了错误

RouteCollection.php 第 161 行中的 NotFoundHttpException:

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您需要定义一个可以为您的文件提供服务的路由。

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

    您可能希望将下载逻辑移至控制器。并添加验证以检查文件是否存在,但您了解基本概念。

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 1970-01-01
      • 2014-08-10
      • 2019-01-24
      • 1970-01-01
      • 2019-05-20
      • 2017-08-08
      • 2015-02-09
      • 1970-01-01
      相关资源
      最近更新 更多