【问题标题】:How to Use Redirect with Response in Laravel如何在 Laravel 中使用重定向和响应
【发布时间】: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


【解决方案1】:

最后,在花了很多时间之后,我首先解决了这个问题带有会话变量的文档 ID 并将此 ID 传递给我的文档下载路径,并在 web.php 和 jquery_download 函数中注册用于下载文档的路径,返回响应下载()。以下是代码

#############插入文档功能
 if(isset($request->document_id))
        {

            $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!");
           Session::flash("successTo", "$document_id");
           return redirect()->back();
            // return Response::download($pathToFile);

        }
#################### 上传刀片
 <script>
    @if(Session::has('successTo'))
    $(document).ready(function () {
        id  = {{ Session::get('successTo') }};
        window.location.href = "/download/documenta/"+id;
    });

    @endif
</script>
#####################WEB.php
 Route::get('/download/documenta/{id}', 'DocumentController@jquery_download')->name('jquery_download');
############### 下载控制器中的文档功能
 public function jquery_download($id)
    {
        $document = Document::find($id)->pluck('docpath')->first();
        echo $pathToFile = public_path()."\\".$document;
        return Response::download($pathToFile);
    }

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2015-05-25
    相关资源
    最近更新 更多