Python是什么类型的flask.request.files.stream应该是?

2024-10-01 09:19:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在Flask(Flask-0.10.1通过pip安装)中,我尝试处理像这样上传的文件

f = flask.request.files[input_name]
stream = f.stream
# then use the stream

但令人困惑的是,在某些情况下,stream是一个BytesIO实例,但同时也是一个file对象的机会。在

我用这种方法测试过

^{pr2}$

它印出来了

<type '_io.BytesIO'>
<type 'file'>

PNG文件来自github:

http://octodex.github.com/images/homercat.pnghttp://octodex.github.com/images/megacat-2.png

我想知道烧瓶为什么会这样。如果我希望上传的数据进入数据库,在这两种情况下都可以调用f.stream.read()?在


Tags: pip文件githubcomhttpflaskstreampng
1条回答
网友
1楼 · 发布于 2024-10-01 09:19:03

小于1024*500字节的文件将写入StringIO对象,而大于该阈值的文件将写入临时文件。在

它是Werkzeug测试框架的一部分,但该功能不是在线文档的一部分:

def stream_encode_multipart(values, use_tempfile=True, threshold=1024 * 500,
                            boundary=None, charset='utf-8'):
    """Encode a dict of values (either strings or file descriptors or
    :class:`FileStorage` objects.) into a multipart encoded string stored
    in a file descriptor.
    """

    ...

Source

相关问题 更多 >