Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Getting file size of file object from file upload in Flask

Tags:

python

file

flask

I've looked through the Flask documentation, both the quickstart guide and the file upload guide, and can't find anything about getting the file size of a file. From what I understand, I'm only given the name of the file from on the users drive, and the name of input field that contained the uploaded file.

Apart from writing the file to the disk and then checking the size with os.path.getsize, is there a way to get the size of a file object?

I've been doing:

f.seek(0,SEEK_END)
f.tell()

There must be a better way right?

like image 851
mowwwalker Avatar asked Apr 23 '12 02:04

mowwwalker


1 Answers

In case of an upload field like <input type="file" name="foo"/> in the HTML page, request.files['foo'] gives you an object, which has an integer attribute called content_length. This is the size of the uploaded file in bytes, see example usage here: https://github.com/dnet/photochecklist/blob/5072331/model.py#L78

like image 64
dnet Avatar answered Oct 12 '22 23:10

dnet