【发布时间】: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 不,它没有。我试过了,但仍然收到数据表错误。
-
<a href="{{ route('reports.download', $fileName) }}"这里是$fileName来的地方。? -
是需要下载的文件名……我想?
-
我在哪里定义它?
-
在您使用它的
view中