1

I am creating an upload system for my flask app. When I upload a zip file I can access it in my app with request.data . However I don't know how to get the size of the file . I've tried request.data.read() but It doesn't work . I get the following error :

'bytes' object has no attribute 'read'

I've also tried request.data.tell() request.data.seek() etc.

Any suggestion ?

John doe
  • 3,372
  • 6
  • 25
  • 51

2 Answers2

1

A simple way to (hopefully) solve your problem is:

  • If you don't want to store the file locally on your device:
import os

file = request.files['file']
file.seek(0, os.SEEK_END)
file_length = file.tell()
# file_length should tell you the size of your .zip file
  • otherwise you could use:
request.files['file'].save('/tmp/file')
file_length = os.stat('/tmp/file').st_size

inspired by that answer :)

Chrissi
  • 151
  • 1
  • 6
0

You can get file size using jquery, then send the file size while sending image.

$('#myFile').bind('change', function() {

  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);
});

If you want to know size using python than you can use os module but you need to save the file first.

 os.path.getsize("/path/isa_005.mp3")
charchit
  • 1,401
  • 2
  • 5
  • 17