I'm trying to send a file from my Flask app through send_file() or send_from_directory() without success.

The request returns a response with status_code=200 but no file is being downloaded. I have verified the functions work as they return errors when the file or directory does not exist

This is the last line of my function. It handles POST requests and should return a file after it has been saved.

# openpyxl stuff above
wb.save(app.instance_path + '/path/to/file/spreadsheet.xlsx') 

return send_file(current_dir + '/path/to/file/spreadsheet.xlsx')

This is what is returned from the server - - [21/Apr/2019 20:05:26] "POST /api/admin/export_bookings HTTP/1.1" 200 -

I verified that the file is indeed being created and saved, and I have verified that the above last line returns an error if the path is wrong or if the file doesn't exist.

Why is this happening?

2 Answers

dariuscosden On Best Solutions

Figured it out. I am using axios to handle my POST requests. It seems javascript POST requests do not have the ability to return files.

I have found a workaround by returning the '/path/to/file/spreadsheet.xlsx' to my javascript as JSON and calling window.open() with that path.

I then only had to create a standard Flask GET route @bp.route('/path/to/file/<filename>) that returns the file from the directory by the url using that send_file() function.

Angelo Mendes On

Your form has enctype = "multipart/form-data"?

Did you check if the file exists in the request?