Flask: send_from_directory, and refresh the page

I use send_from_directory in a jar that will allow the user to download a specific file. I want to ensure that the page refreshes after the file is uploaded, but I don’t know how I can achieve this using a flask (if at all possible). Currently my code is as follows:

if report: location = Document_Generator(report).file_location return send_from_directory(location[0], location[1], as_attachment=True) 

So my question is: how to refresh the page (return a normal response using the template), and also allow the user to download the file?

0
python flask
source share
1 answer

HTTP does not allow you to do what you want to do (200 + 300). You can do this at the client level, but 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(); }; } 
+1
source share

All Articles