【发布时间】:2019-09-20 17:21:05
【问题描述】:
如何在 laravel 中使用重定向和响应。下载后我正在尝试下载一些文档我想重定向页面以获得烤面包机成功消息。
if(isset($request->document_id))
{
echo "Set";
$document = Document::find($request->document_id)->pluck('docpath')->first();
echo $document;
echo $document_id = $request->document_id;
echo $pathToFile = public_path()."\\".$document;
Session::flash("success", "Your File Downloaded Successfully!");
return redirect()->back();
return Response::download($pathToFile);
}
但它不起作用,我们如何才能做到这一点,或者我们可以在响应中使用重定向。
【问题讨论】:
-
如果不起作用请告诉我..
-
不,它不工作,它显示以下错误调用未定义的方法 Symfony\Component\HttpFoundation\BinaryFileResponse::with()
-
我认为您不能同时重定向和下载(作为响应)。如果您需要显示消息以及下载文件,您可以使用 ajax。示例代码 return response()->json([ 'message' => 'This is message test', 'pathToFile' => $pathToFile, ]);在 ajax 代码中:成功:function(result) { alert(result.message); window.open(result.pathToFile, '_blank'); },
-
有没有其他方法可以下载文件并重定向回页面??
-
不,只能有一个响应。在您下载的文件是响应。
标签: laravel response response.redirect