1

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
2. Redirect to a new page

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.

Afendas
  • 31
  • 2

2 Answers2

0

You can use header() to send an HTTP Location header

header('Location'.'new url');
mzea
  • 26
  • 5
0

Using those methods, you cannot redirect after sending a response, since sending a response will terminate the HTTP connection:

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

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.

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.

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. 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.

Jan Tojnar
  • 5,306
  • 3
  • 29
  • 49