Python Flask:获取 request.files 对象的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15772975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask: Get the size of request.files object
提问by saidozcan
i want to get the size of uploading image to control if it is greater than max file upload limit.I tried this one:
我想控制上传图片的大小是否大于最大文件上传限制。我试过这个:
@app.route("/new/photo",methods=["POST"])
def newPhoto():
form_photo = request.files['post-photo']
print form_photo.content_length
It printed 0Where am i doing wrong? Should i find the size of this image from the temp path of it? Isn't there anything like PHP's $_FILES['foo']['size']in Python?
它打印0我哪里做错了?我应该从它的临时路径中找到这个图像的大小吗?$_FILES['foo']['size']Python 中没有类似 PHP 的东西吗?
采纳答案by DazWorrall
There are a few things to be aware of here - the content_length property will be the content length of the file upload as reported by the browser, but unfortunately many browsers dont send this, as noted in the docsand source.
这里有几件事情需要注意 - content_length 属性将是浏览器报告的文件上传的内容长度,但不幸的是,如docs和source中所述,许多浏览器不会发送它。
As for your TypeError, the next thing to be aware of is that file uploads under 500KB are stored in memory as a StringIO object, rather than spooled to disk (see those docs again), so your stat call will fail.
至于您的 TypeError,接下来要注意的是,500KB 以下的文件上传作为StringIO 对象存储在内存中,而不是假脱机到磁盘(再次参见这些文档),因此您的 stat 调用将失败。
MAX_CONTENT_LENGTH is the correct way to reject file uploads larger than you want, and if you need it, the only reliable way to determine the length of the data is to figure it out after you've handled the upload - either stat the file after you've .save()d it:
MAX_CONTENT_LENGTH 是拒绝大于您想要的文件上传的正确方法,如果您需要它,确定数据长度的唯一可靠方法是在您处理上传后弄清楚 - 要么在您之后统计文件已经.save()做到了:
request.files['file'].save('/tmp/foo')
size = os.stat('/tmp/foo').st_size
Or if you're not using the disk (for example storing it in a database), count the bytes you've read:
或者,如果您不使用磁盘(例如将其存储在数据库中),请计算您读取的字节数:
blob = request.files['file'].read()
size = len(blob)
Though obviously be careful you're not reading too much data into memory if your MAX_CONTENT_LENGTH is very large
尽管显然要小心,如果您的 MAX_CONTENT_LENGTH 非常大,您不会将太多数据读入内存
回答by Michael Pratt
The proper way to set a max file upload limit is via the MAX_CONTENT_LENGTHapp configuration. For example, if you wanted to set an upload limit of 16 megabytes, you would do the following to your app configuration:
设置最大文件上传限制的正确方法是通过MAX_CONTENT_LENGTH应用程序配置。例如,如果您想将上传限制设置为 16 兆字节,您可以对应用程序配置执行以下操作:
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
If the uploaded file is too large, Flask will automatically return status code 413 Request Entity Too Large - this should be handled on the client side.
如果上传的文件太大,Flask 会自动返回状态码 413 Request Entity Too Large - 这应该在客户端处理。
回答by codegeek
As someone else already suggested, you should use the
正如其他人已经建议的那样,您应该使用
app.config['MAX_CONTENT_LENGTH']
to restrict file sizes. But Since you specifically want to find outthe image size, you can do:
限制文件大小。但是由于您特别想找出图像大小,您可以执行以下操作:
import os
photo_size = os.stat(request.files['post-photo']).st_size
print photo_size
回答by Steely Wing
If you don't want save the file to disk first, use the following code, this work on in-memory stream
如果您不想先将文件保存到磁盘,请使用以下代码,这适用于内存流
import os
file = request.files['file']
file.seek(0, os.SEEK_END)
file_length = file.tell()
otherwise, this will better
否则,这会更好
request.files['file'].save('/tmp/file')
file_length = os.stat('/tmp/file').st_size
回答by Aniket
The following section of the code should meet your purpose..
代码的以下部分应该符合您的目的..
form_photo.seek(0,2) size = form_photo.tell()

