【发布时间】:2019-03-04 16:28:50
【问题描述】:
我在 Vue 中有一个多步骤表单,一旦我收集了所有信息,我就会将结果发布到 Laravel 控制器。这是该站点的经过身份验证的区域。我正在使用护照。所以基本上我有一个 Vue SPA,它是在 Laravel 5.7 框架内构建的网站的管理区域。
Vue file:
axios.post('/api/quotes/finalize', this.wizardModel)
.then(response => {
if (response.data.success) {
//
}
})
.catch(err => {
if (err.response.status == 401) {
window.location.href = '/login';
}
swal.showValidationError(
`Request failed: ${error}`
)
})
控制器获取数据并制作 pdf。所有这些都在起作用。然后,它需要查看三个操作 - 通过电子邮件发送 PDF、通过 CC 发送 PDF 以及下载 PDF。
public function finalizeQuote(Request $request)
{
$data = $request->all();
$data['phone'] = $this->formatTelephone($data['phone']);
$dateStamp = date('Ymdhis', strtotime('now'));
$fileName = 'quote-' . $dateStamp . '.pdf';
$html = View::make('quotes.quote', compact('data'))->render();
$conv = new Converter();
$conv->addPage($html)
->save('storage/' . $fileName);
if ($data['actions']['emailPDF']) {
$message = (new Proposal('storage/' . $fileName))
->onConnection('redis')
->onQueue('emails');
if ($data['actions']['carbonCopy']) {
Mail::to($data['emailAddress'])
->cc(Auth::user()->email)
->queue($message);
} else {
Mail::to($data['emailAddress'])->queue($message);
}
}
if ($data['actions']['downloadPDF']) {
return response()->download(public_path('storage/' . $fileName));
}
}
所以在开发工具中,我在响应中看到了 pdf 文件。浏览器中不会发生下载。我确信我只是在这里遗漏了一些基本的东西。
【问题讨论】:
-
下载请求的响应头中的内容类型是什么?
-
application/pdf 是 Chrome 开发工具中“响应标头”部分下的内容类型。在“请求标头”下是 application/json;charset-UTF-8.
-
好的,响应标头中是否存在
Content-Disposition标头?如果没有,请将Content-Disposition: attachment; filename="filename.pdf"添加到响应标头。如果 Content-Disposition 标头存在,请告诉我它的值是什么。 -
内容配置:附件; filename=quote-20180928035845.pdf 是已经存在的;这是正确的文件名
-
有趣...您确定要返回的路径中有一个文件吗?还有 $data['actions'] 的价值是什么?