在 Laravel 中下载后如何重定向?
问题描述
我正在使用 maatwebsite/excel 库创建 excel 文件,然后下载我的文件.
I'm using maatwebsite/excel library to create excel files then download my file.
在我的控制器中,我这样做:
In my controller I do like this:
这是我生成excel文件的功能:
That is my function to generate excel file :
但是在我下载 excel 文件后,我无法重定向到路线或视图,我也尝试使用:
But after I download the excel file I cant redirect to a route or view and I Also tried to use:
路线:
过滤器:
但是还是不行,下载我的文件后有没有其他方法可以重定向?
But it didn't work anyway, is there another way to redirect after downloading my file?
推荐答案
做不到.问题在于,如果您向用户浏览器发送下载指令,实际上您是在发送响应,而您只能返回一个响应.
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:
然后在您的新页面中,您将有一些选项:
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:
另外,看看这个答案:PHP 生成文件下载然后重定向
这篇关于在 Laravel 中下载后如何重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!