Allow partial content on bytesio
This commit is contained in:
parent
4f3dbb3f3b
commit
b570bf699c
2 changed files with 27 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue