Reputation: 5162
I am testing for cleaned_data['content'].size > MAX_SIZE
in forms to validate the upload size.
When googling how to check the length for an upload in Django, I came across several places (links below) where people are using some format (seemingly based on powers of 2 somehow) to represent MBs in bytes.
I guess there must be a good reason.
Why use such a format ? And how do you calculate the values below? (taken from the linked SO question)
# 2.5MB - 2621440
# 5MB - 5242880
# 10MB - 10485760
# 20MB - 20971520
# 50MB - 5242880
# 100MB 104857600
# 250MB - 214958080
# 500MB - 429916160
Django File upload size limit
https://djangosnippets.org/snippets/1303/
https://django-filebrowser.readthedocs.org/en/3.5.2/settings.html
Upvotes: 1
Views: 787
Reputation: 33833
just because a kilobyte is 1024 bytes... with bytes the 'round numbers' are powers of 2 rather than 10
strictly speaking 1024 bytes is a "kibibyte" and according to SI units, a "kilobyte" is 1000 bytes, just like a kilogram is 1000 grams
however operating systems such as Windows tend to use the former definition
So for example if you want to limit upload size to 2MB you should use 2 * 2**20
(or 2 x 1024**2
)
or general form for n MB
would be n * 1024**2
http://en.wikipedia.org/wiki/Megabyte#Definitions
Upvotes: 1