python - How to change downloading name in flask? -


i have small project has response files. know using nginx better decision files small.

part of program:

return send_file(os.path.join(filepath, filename)) 

that line returns file has filename download without format or that. filename has been downloaded same , doesn't depend on real name of file. real name of file table.csv. how can return file correct filename ?

you need set content-disposition: attachment; filename=.... http header browser use correct filename.

you can have send_file() set header setting as_attachment=true argument. filename taken file object passed in. use attachment_name argument explicitly set different filename:

return send_file(os.path.join(filepath, filename), as_attachment=true) 

from flask.send_file documentation:

  • as_attachment – set true if want send file content-disposition: attachment header.
  • attachment_filename – filename attachment if differs file’s filename.

you may want use flask.send_from_directory() function instead. function first ensures filename exists (raising notfound if not), , ensures filename doesn't contain .. relative elements might used 'escape' directory. use filenames sourced untrusted sources:

return send_from_directory(filepath, filename, as_attachment=true) 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -