How do I redirect after download in Laravel?

It cannot be done. The problem is that if you send a download instruction to the user browser, you are, as a matter of fact, sending a response and you can send only one response back.

What you could do is to, first redirect your user to the “final” page and in that page start the download. The code would be something like:

Session::flash('download.in.the.next.request', 'filetodownload.pdf');

return Redirect::to('/whatever/page');

Then in your new page you’ll have some options:

So you can in your layout do something like:

<html>
  <head>
      @if(Session::has('download.in.the.next.request'))
         <meta http-equiv="refresh" content="5;url={{ Session::get('download.in.the.next.request') }}">
      @endif
   <head>

   <body>
      ...
   </body>
</html>

Also, take a look at this answer: PHP generate file for download then redirect

Leave a Comment