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– settrueif want send filecontent-disposition: attachmentheader.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
Post a Comment