How to Redirect After Download in Laravel

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:

  • HTML: [meta http-equiv="refresh" content="5;url=http://watever.com/create_csv.php"]
  • Javascript: location.href = 'http://watever.com/create_csv.php';
  • iframe: [iframe src="create_csv.php"][/iframe]

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

laravel return pdf download but still want to redirect

No, you can't. For the redirection is done by client side, it's a small js code generate by server side. When the request is done, the connection is closed. Server side can not send info to browser any more.
you may try open a new tab for download and you redirect the main tab.

PHP generate file for download then redirect

I don't think this can be done - although I am not 100% sure.

The common thing (e.g. in popular download sites) is the reverse: first you go to the after page and then the download starts.

So redirect your users to the final page that (among other things) says:

Your download should start automatically. If not click [a href="create_csv.php"]here[/a].

As about initiating the download (e.g. automatically calling create_csv.php) you have many options:

  • HTML: [meta http-equiv="refresh" content="5;url=http://site/create_csv.php"]
  • Javascript: location.href = 'http://site/create_csv.php';
  • iframe: [iframe src="create_csv.php"][/iframe]


Related Topics



Leave a reply



Submit