<p>The <code>{% header %}</code> tag probably isn't what you want then - they must be rendered first, as they affect what the webserver is returning to the browser in the page headers. Because you're setting the location header to the file url, it is telling the <em>browser</em> to redirect to that instead of rendering the rest of the page (which would still technically be rendered by the server, just never seen by the browser).</p>

<p>It sounds like the better option would be to trigger the redirect to the file location via javascript after the page loads, <a href="https://stackoverflow.com/questions/32016910/download-file-on-page-load-not-working">similar to this approach</a>.</p>

CC BY-SA 4.0
2020-04-16T08:48:54.157Z
Thanks, that did it. 
Tyssen
CC BY-SA 4.0
2020-04-16T07:30:43.747Z
2020-04-16T07:53:45.107Z
2020-04-16T07:53:45.107Z
michaelroper
<p>I have a page that is supposed to render the content and then force a download of a file.</p>

<p>I have <code>{% header "Location: " ~ entry.files[0].url %}</code> in my template which brings up the browser's save dialogue window, but then the page doesn't render.</p>

<p>Is there a way I can delay the setting of the <code>{% header %}</code> tag until after the page has rendered? Or is there a better way of doing what I'm trying to achieve?</p>

<download>
Force download a file on page load
# Force download a file on page load

(Asked by: username_0 on 2020-04-16)

I have a page that is supposed to render the content and then force a download of a file.

I have `{% header "Location: " ~ entry.files[0].url %}` in my template which brings up the browser's save dialogue window, but then the page doesn't render.

Is there a way I can delay the setting of the `{% header %}` tag until after the page has rendered? Or is there a better way of doing what I'm trying to achieve?

**Tags:** download


---

## 1 answers

### Answer by username_1 on 2020-04-16. Score: 2 | Accepted answer

The `{% header %}` tag probably isn't what you want then - they must be rendered first, as they affect what the webserver is returning to the browser in the page headers. Because you're setting the location header to the file url, it is telling the *browser* to redirect to that instead of rendering the rest of the page (which would still technically be rendered by the server, just never seen by the browser).

It sounds like the better option would be to trigger the redirect to the file location via javascript after the page loads, [similar to this approach](https://stackoverflow.com/questions/32016910/download-file-on-page-load-not-working).

> **Comments:**
> **username_0**: Thanks, that did it.   

---

