send_file(filename_or_fp, mimetype=None, as_attachment=False, attachment_filename=None, add_etags=True, cache_timeout=None, conditional=False)¶
Sends the contents of a file to the client. This will use the most efficient method available and configured. By default it will try to use the WSGI server’s file_wrapper support. Alternatively you can set the application’s
Trueto directly emit an
X-Sendfileheader. This however requires support of the underlying webserver for
By default it will try to guess the mimetype for you, but you can also explicitly provide one. For extra security you probably want to send certain files as attachment (HTML for instance). The mimetype guessing requires a filename or an attachment_filename to be provided.
Please never pass filenames to this function from user sources; you should use
New in version 0.2.
New in version 0.5: The add_etags, cache_timeout and conditional parameters were added. The default behavior is now to attach etags.
Changed in version 0.7: mimetype guessing and etag support for file objects was deprecated because it was unreliable. Pass a filename if you are able to, otherwise attach an etag yourself. This functionality will be removed in Flask 1.0
Changed in version 0.9: cache_timeout pulls its default from application config, when None.
- filename_or_fp – the filename of the file to send in latin-1.
This is relative to the
root_pathif a relative path is specified. Alternatively a file object might be provided in which case
X-Sendfilemight not work and fall back to the traditional method. Make sure that the file pointer is positioned at the start of data to send before calling
- mimetype – the mimetype of the file if provided, otherwise auto detection happens.
- as_attachment – set to
Trueif you want to send this file with a
- attachment_filename – the filename for the attachment if it differs from the file’s filename.
- add_etags – set to
Falseto disable attaching of etags.
- conditional – set to
Trueto enable conditional responses.
- cache_timeout – the timeout in seconds for the headers. When
None(default), this value is set by
- filename_or_fp – the filename of the file to send in latin-1. This is relative to the