2

How to use redirect with response in laravel. i am trying to download some document after downloading i want to redirect the page for toaster success mesasge.

 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);

        }

but it is not working how can we do this or we can use redirect with the response.

Alessio
  • 3,404
  • 19
  • 35
  • 48
Adnan Ali
  • 76
  • 1
  • 8
  • let me know if doesn't work.. – Vikas Rinvi May 02 '19 at 08:18
  • No its not working and it display the following error Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::with() – Adnan Ali May 02 '19 at 08:24
  • I don't think you can Redirect and download(as response) at the same time. You can use ajax if you need to show message as well as download file. Sample Code return response()->json([ 'message' => 'This is message test', 'pathToFile' => $pathToFile, ]); In ajax code: success: function(result) { alert(result.message); window.open( result.pathToFile, '_blank'); }, – azm_shah May 02 '19 at 08:32
  • is there any other way to download the file and redirect back to page?? – Adnan Ali May 02 '19 at 08:32
  • No, there can be one response. In your downloading the file is the response. – azm_shah May 02 '19 at 08:34
  • okay my bad .. try this solution https://stackoverflow.com/questions/25624927/how-do-i-redirect-after-download-in-laravel – Vikas Rinvi May 02 '19 at 08:47
  • Possible duplicate of [How do I redirect after download in Laravel?](https://stackoverflow.com/questions/25624927/how-do-i-redirect-after-download-in-laravel) – party-ring May 02 '19 at 09:31
  • BTW Vikas Rinvi Thanks for your time and suggestions – Adnan Ali May 02 '19 at 11:57

1 Answers1

0

Finally after consuming much time on it i have got solution to that problem firstly i have set a session veritable with document id in my controller and redirect back to upload.blade.php page and in the blade page section of script i got the document id with session variable and pass this id to my document download route and register a route for downloading document in web.php and in jquery_download function return the response download(). Following is the code

############# insert Document Function
 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);

        }
##################### Upload Blade
 <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');
################ DOWNLOAD DOCUMENT FUNCTION IN CONTROLLER
 public function jquery_download($id)
    {
        $document = Document::find($id)->pluck('docpath')->first();
        echo $pathToFile = public_path()."\\".$document;
        return Response::download($pathToFile);
    }
Adnan Ali
  • 76
  • 1
  • 8