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
127.0.0.1 - - [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?
Your form
has enctype = "multipart/form-data"
?
Did you check if the file exists in the request?
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.
I also came across this kind of problem. I am also using flask and send_file() library to send the file to UI for a user to download.
I was just sending file using flask as below.
@app.run('/download_any_file',methods=['POST'])
def download():
path='folder_path_where_file_exist'
filename='abcde.xlsx' # i am getting this filename from UI
full_path=path+'/'+ filename
return send_file(full_path,as_attachment=True)
I was getting some response from this API but UI was not able to consume this response for some reason.
added a snip of Response I was getting with above code:
After going through documentation https://tedboy.github.io/flask/generated/flask.send_file.html , i found that one parameter 'mimetype' for excel file(.xlsx) or (.xls) should be passed as a parameter. Note: mimetype for (.xlsx and .xls) are different plz follow this link to find mimetype for different files https://docs.microsoft.com/en-us/archive/blogs/vsofficedeveloper/office-2007-file-format-mime-types-for-http-content-streaming-2 .
I changed my code finally as below:
@app.run('/download_any_file',methods=['POST'])
def download():
path='folder_path_where_file_exist'
filename='abcde.xlsx' # i am getting this filename from UI
full_path=path+'/'+ filename
return send_file(full_path,as_attachment=True,mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
Now same mime-type should be used in the UI side to catch and decode the response and put it in excel file to download. In UI contentType should be the same mimetype which is used in flask send_file() contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
This way you can download an excel file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.