Flask / Werkzeug如何将HTTP内容长度标题附加到文件下载 [英] Flask/Werkzeug how to attach HTTP content-length header to file download
问题描述
我使用的是Flask(基于Werkzeug),它使用Python。
用户可以下载一个文件,我使用 send_from_directory
功能。 p>
但是,当实际下载文件时,HTTP头 content-length
未设置。所以用户不知道下载的文件有多大。
我可以使用 os.path.getsize(FILE_LOCATION)
在Python中获取文件大小(以字节为单位),但是找不到在Flask中设置 content-length
头的方法。
任何想法?
我相信你会这样做(未经测试):
从烧瓶导入响应
response = Response()
response.headers.add('content-length ',str(os.path.getsize(FILE_LOCATION)))
请参阅: Werkzug的Headers对象和 Flask的Response对象。
I am using Flask (based on Werkzeug) which uses Python.
The user can download a file, I'm using the send_from_directory
-function.
However when actually downloading the file, the HTTP header content-length
is not set. So the user has no idea how big the file being downloaded is.
I can use os.path.getsize(FILE_LOCATION)
in Python to get the file size (in bytes), but cannot find a way to set the content-length
header in Flask.
Any ideas?
I believe you'd do something like this (untested):
from flask import Response
response = Response()
response.headers.add('content-length', str(os.path.getsize(FILE_LOCATION)))
See: Werkzug's Headers object and Flask's Response object.
这篇关于Flask / Werkzeug如何将HTTP内容长度标题附加到文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!