【发布时间】:2021-08-02 03:59:40
【问题描述】:
如何在新标签页中从 s3 打开 pdf 文件?
以下代码下载文件..但我想在浏览器中查看它
在控制器中存储方法
public function store(Request $request)
{
$validated = $request->validate([
'leg_number' => 'required',
'leg_year' => 'required',
'leg_title' => 'required',
'leg_type_id' => 'required',
'leg_published' => 'required',
'file' => 'required',
]);
$name = $request->leg_number . '_' . $request->leg_year . '.pdf';
$path = 'documents/' . $name;
Storage::disk('s3')->put($path, $request->file, 'public');
$validated['leg_path'] = $name;
$document = Legislation::create($validated);
}
在控制器中显示方法
public function showLegislation(Legislation $legislation)
{
$content = Storage::disk('s3')->get('documents/' . $legislation->leg_path);
$header = [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $legislation->leg_path . '"'
];
return Response::make($content, 200, $header);
}
路线
Route::get('legislation/show/{legislation}', [LegislationController::class, 'showLegislation'])->name('legislation.show');
刀片
<a href="{{ route('admin.legislation.show', $leg->id) }}" target="_blank"></a>
【问题讨论】:
-
您希望 pdf 显示在浏览器中而不是下载它?
-
是的..这正是我想要的
-
您是否尝试将
Content-Disposition标头设置为inline? -
我试过..但同样的事情...正在下载
-
我发现问题与 chrome 浏览器有关......因为 Firefox 按我的意愿在新选项卡中打开文件!