首页 > 解决方案 > 为什么我在尝试下载文件时出现此页面错误?

问题描述

嗨,我在尝试下载上传的文件时出错。我正在使用 laravel 7。错误是找不到 404 页面,可能是因为目录不匹配。

这是我的控制器中的存储方法:

public function store(Request $request){

      $detalleTicket = new DetalleTicket();

      if($request->file('file')){
         $file = $request->file('file');
         $filename = time().'.'.$file->getClientOriginalName();
         $request->file->move('storage/', $filename);
         $detalleTicket->file = $filename;
       }      
       

       $detalleTicket->save();
       Session::flash('success');
       return redirect()->route('detalles-tickets.view');
   }

这是我在控制器中的下载方法:

public function download($file){
    return response()->download('storage/'.$file);
}

我的目录是 public->storage->(每个文件)

这是我在 web.php 中的路线:

Route::get('/download/{file}', 'Backend\DetalleTicketController@download')->name('detalles-tickets.download');

这是我的下载按钮:

<a title="Download" id="download" class="btn btn-sm btn-success"
                                        href="/detalles-tickets/download/{{ $detalleTicket->file }}"><i
                                        class="fa fa-download"></i></a>

有谁知道为什么单击按钮我找不到 404 页面?

标签: laravellaravel-7

解决方案


@if($detalleTicket->file)
<a
    title="Download"
    id="download"
    class="btn btn-sm btn-success"
    href="{{ route('detalles-tickets.download', ['file' => $detalleTicket->file]) }}">
    <i class="fa fa-download"></i>
</a>
@endif

推荐阅读