I am currently working on figuring out how to trigger a file download dialog box after receiving an Ajax request from the Flask server using Axios.
Here is my current client-side code snippet:
<script>
export default {
...
exportCSV: function() {
axios.post('/exportdata',
{
data: this.data,
},
{
headers: {
'Content-Type': ' text/html; charset=utf-8'
}
}
)
.then((response) => {
var headers = response.headers
var blob = new Blob([response.data], {type: headers['content-type']})
var link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = 'Filename'
link.click()
})
.catch(e => {
console.log('Error')
})
}
}
For my server-side code, I have implemented the following:
return Response(
json.dumps({'json_data': my_data}, cls=MyEncoder),
status=200,
headers = {
"Content-Disposition": "attachment; filename={0}".format(filename),
}
mimetype="application/csv")
Although I receive a successful response from the server with valid header data, unfortunately, I am unable to open the file download dialog box. Any suggestions or guidance would be greatly appreciated. Thank you!