Skip to content Skip to sidebar Skip to footer

How To Download A Csv File Requested Through Jquery/ajax From Flask Server

I am building a python flask web app. I am trying to get a CSV file downloaded through jquery/ ajax call. This is how my ajax request looks like: $('.download-btn').on('click', fun

Solution 1:

I think it's because the file's content is stored in the data variable (of the "success" callback of $.ajax), so it's just a js variable for the browser.

I think the best (cleanest maybe?) thing you can do is wrap your .download-btn in a <a> tag with your download url in href

<ahref="/downloadFile"target="_blank"><buttonclass="download-btn">Download</button></a>

This way "the browser itself" makes the GET request to /downloadFile (in a new tab because of the target="_blank" attribute), receives the stream and downloads the file (and closes tab once the download is started).

Post a Comment for "How To Download A Csv File Requested Through Jquery/ajax From Flask Server"