Allow partial content on bytesio

This commit is contained in:
Ignasi Bosch 2018-10-18 23:30:03 +01:00 committed by David Lord
parent 4f3dbb3f3b
commit b570bf699c
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
2 changed files with 27 additions and 1 deletions

View file

@ -8,7 +8,7 @@
:copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details.
"""
import io
import os
import socket
import sys
@ -512,6 +512,8 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
.. versionchanged:: 1.1
Filenames may be a `PathLike` object.
.. versionadded:: 1.1
Partial content supports ``BytesIO``.
:param filename_or_fp: the filename of the file to send.
This is relative to the :attr:`~Flask.root_path`
@ -600,6 +602,12 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
mtime = os.path.getmtime(filename)
fsize = os.path.getsize(filename)
headers['Content-Length'] = fsize
elif isinstance(file, io.BytesIO):
try:
fsize = file.getbuffer().nbytes
except AttributeError:
fsize = len(file.getvalue())
headers['Content-Length'] = fsize
data = wrap_file(request.environ, file)
rv = current_app.response_class(data, mimetype=mimetype, headers=headers,