0

I've written an API to return a CSV to a user based on content they fill into a form in a web application. When running the code below, I can see the response in my console and am certain that the content is properly being returned by my API, but I can't seem to get the file to automatically start downloading.

csv = final_df.to_csv()
response = make_response(csv)
response.headers['Content-Disposition'] = "attachment; filename=export.csv"
response.headers['Content-type'] = "application/force-download"
return response
bad_coder
  • 11,289
  • 20
  • 44
  • 72

1 Answers1

0

A working example of mine differs by quoting the filename (which the Developer Docs hint at being required), and using a correct mimetype.

Try

return bytes(csv, encoding='UTF-8'), 200, {
    'Content-Type': 'text/csv',
    'Content-Disposition': 'attachment; filename="export.csv"'}
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • Unfortunately still no luck with the forced download with this solution. Tested in Chrome, IE, and Firefox. – ashpplejack Jul 30 '20 at 14:49
  • I noticed the code I pulled that from was still Python2.7, and made one change (to the bytes() call) for Python3. It works for me on Chrome and Firefox. – Dave W. Smith Jul 30 '20 at 16:36
  • Thanks Dave. I'm wondering if maybe there's something going on with my network settings that might be preventing the download, since I also added an encoding to get the solution to run. I ended up keeping your solution to return my response, but had to write something client side to prompt the file download. – ashpplejack Jul 31 '20 at 17:25