[英]Nette Redirect and Send file in the same form
I have a problem with sending a file and redirect to new page in the same time, now it is working just two ways:我在发送文件并同时重定向到新页面时遇到问题,现在它只有两种工作方式:
1. Sending a file via sendResponse 1.通过sendResponse发送文件
2. Redirect to a new page 2.重定向到新页面
My code after form successed:表单成功后我的代码:
$this->sendResponse(new FileResponse($file));
$this->redirect('this', ['Id' => NULL, 'addAge' => NULL]);
I would like to know how to send a file and also redirect to new page, I tried to make a link to Download Presenter but it didn't work anyway.我想知道如何发送文件并重定向到新页面,我试图创建一个指向下载演示者的链接,但它仍然不起作用。
You can use header()
to send an HTTP Location header您可以使用
header()
发送 HTTP 位置 header
header('Location'.'new url');
Using those methods, you cannot redirect after sending a response, since sending a response will terminate the HTTP connection:使用这些方法,您无法在发送响应后重定向,因为发送响应将终止 HTTP 连接:
https://github.com/nette/application/blob/7177fcdb5edbf9fc90c78dc9763d6bd2cc1cc452/src/Application/UI/Presenter.php#L613 https://github.com/nette/application/blob/7177fcdb5edbf9fc90c78dc9763d6bd2cc1cc452/src/Application/UI/Presenter.php#L613
and you cannot send response after redirecting, since that terminates the connection as well:并且您无法在重定向后发送响应,因为这也会终止连接:
https://github.com/nette/application/blob/7177fcdb5edbf9fc90c78dc9763d6bd2cc1cc452/src/Application/UI/Component.php#L286 https://github.com/nette/application/blob/7177fcdb5edbf9fc90c78dc9763d6bd2cc1cc452/src/Application/UI/Presenter.php#L660 https://github.com/nette/application/blob/7177fcdb5edbf9fc90c78dc9763d6bd2cc1cc452/src/Application/UI/Component.php#L286 https://github.com/nette/application/blob/7177fcdb5edbf9fc90c78dc9763d6bd2cc1cc452/src/Application/UI/Presenter .php#L660
What is worse, downloading is typically (and also in this case ) done using Content-Disposition
HTTP header , which tells the browser to download the body of the HTTP response under supplied file name.更糟糕的是,下载通常( 在这种情况下也是如此)使用
Content-Disposition
HTTP header 完成,它告诉浏览器在提供的文件名下下载 Z293C9EA246FF9985DC6F62A650F78986 响应的正文。
On the other hand, redirection is done ( here too ) using Location
header , which tells the browser that it should try its luck in a different location so it will not even check the body.另一方面,重定向是使用
Location
header完成的( 这里也是),它告诉浏览器它应该在不同的位置试试运气,这样它甚至不会检查正文。
These two use cases inherently clash.这两个用例本质上是冲突的。
I would suggest adding <meta http-equiv="refresh">
on the target page to redirect to a route that would send the file response.我建议在目标页面上添加
<meta http-equiv="refresh">
以重定向到将发送文件响应的路由。 This should make user remain on the target page you wanted to redirect to, while prompting them to download the attachment.这应该使用户留在您要重定向到的目标页面上,同时提示他们下载附件。 Many download sites do it like this.
许多下载网站都是这样做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.