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
How to Get Int Instead String from Form
How to Get Insert Id in Mssql in PHP
Remove Diacritics from a String
How to Get Unique Value in Multidimensional Array
Access Denied for User 'Root'@'Localhost' with PHPmyadmin
The Csrf Token Is Invalid. Please Try to Resubmit the Form
Extracting All Values Between Curly Braces Regex PHP
How to Destroy the Session Cookie Correctly with PHP
How to Redirect After Download in Laravel
Laravel 5 Not Finding CSS Files
Codeigniter - Why Use Xss_Clean
Php: How to Resolve a Relative Url
Nginx Redirect Loop, Remove Index.PHP from Url