【问题标题】:Laravel 8 upload and download fileLaravel 8 上传下载文件
【发布时间】:2021-04-14 23:08:13
【问题描述】:

我试图在 Laravel 的 dataTable 中放置一个下载按钮,但出现以下错误:“DataTables 警告:table id=dataTableBuilder - 异常消息:未定义的变量:fileName”。

这是我的 ReportController sn-p:


public function store(CreateReportRequest $request)
    {
        $request->validate([
            'file_path' => 'required|mimes:pdf|max:2048'
            ]);
    
        $report = new Report;
            if($request->file()) {
                $fileName           = time().'_'.$request->file_path->getClientOriginalName();
                $filePath           = $request->file('file_path')->storeAs('reports', $fileName, 'public');
                $report->name          = time().'_'.$request->file_path->getClientOriginalName();
                $report->file_path     = '/storage/' . $filePath;
                $report->save();
                Flash::success('Pop saved successfully.');
            }

        return redirect(route('reports.index'));
    }

    public function download($fileName)
    {
            $file_path = storage_path('reports') . "/" . $fileName;
            return Response::download($file_path);
    }

这是我的观点 sn-p(下载按钮):

    <a href="{{ route('reports.download', $fileName) }}" class='btn btn-ghost-info'>
       <i class="fa fa-download"></i>
    </a>

这是我的路线 sn-p:

    Route::post('/reportdownload/{fileName}', [App\Http\Controllers\ReportController::class, 'download'])->name('reports.download');

请帮我弄清楚我做错了什么。

【问题讨论】:

  • @KamleshPaul 不,它没有。我试过了,但仍然收到数据表错误。
  • &lt;a href="{{ route('reports.download', $fileName) }}" 这里是$fileName 来的地方。?
  • 是需要下载的文件名……我想?
  • 我在哪里定义它?
  • 在您使用它的view

标签: laravel datatable


【解决方案1】:

尝试在 web.php 中捕获带有 {ID} 的文件并同时查看 例如:WEB.PHP

Route::post('/reportdownload/{id}', [App\Http\Controllers\ReportController::class, 'download'])->name('reports.download');

查看:

<a href="{{ route('reports.download', $id) }}" class='btn btn-ghost-info'>
   <i class="fa fa-download"></i>
</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-16
    • 2021-11-27
    • 1970-01-01
    • 2021-09-13
    • 2021-05-02
    • 2017-09-09
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多