Reputation: 6881
I am using send_from_directory in flask which will allow a user to download a certain file. What I want to achieve is that the page refreshes after the file is downloaded, but I'm unsure how I can achieve this with flask (if at all possible). Currently my code looks like this:
if report:
location = Document_Generator(report).file_location
return send_from_directory(location[0],
location[1], as_attachment=True)
So my question is : How can I refresh the page (return a normal response with a template) as well as allowing the user to download the file?
Upvotes: 2
Views: 4220
Reputation: 159865
HTTP doesn't let you do what you want to do (200 + 300). You can do it at the client level however using _target
+ JavaScript (or just JavaScript).
<a href="/path/to/download/file" target="downloadIframe">Download file</a>
<iframe id="downloadIframe"></iframe>
Combined with some JavaScript:
var slice = Array.prototype.slice;
var links = document.querySelectorAll("[target='downloadIframe']"),
iframe = document.getElementById("downloadIframe");
slice.call(links).forEach(function(link) {
link.addEventListener("click", reloadPageOnIframeLoad);
});
function reloadPageOnIframeLoad() {
// Reset this for each click on a download link
// rather than adding another event listener each time.
iframe.onload = function() { window.location.reload(); };
}
Upvotes: 2