Aidas K
Aidas K

Reputation: 171

Django: FILE_UPLOAD_MAX_MEMORY vs DATA_UPLOAD_MAX_MEMORY

I am working with Django 2.2 and got stuck with file upload size validation. I have read django documentation:

DATA_UPLOAD_MAX_MEMORY_SIZE
FILE_UPLOAD_MAX_MEMORY_SIZE

I only set DATA_UPLOAD_MAX_MEMORY (to 20 MB), as mentioned in documentation:

The check is done when accessing request.body or request.POST and is calculated against the total request size excluding any file upload data.

But in my project it also checks my uploading file size in request.FILES.

Can someone explain differences between FILE_UPLOAD_MAX_MEMORY and DATA_UPLOAD_MAX_MEMORY? And how to use them properly?

Upvotes: 5

Views: 8807

Answers (2)

yanqzhi
yanqzhi

Reputation: 412

DATA_UPLOAD_MAX_MEMORY

The check is done when accessing request.body or request.POST and is calculated against the total request size excluding any file upload data.

FILE_UPLOAD_MAX_MEMORY_SIZE

The maximum size (in bytes) that an upload will be before it gets streamed to the file system. If uploading files are larger than FILE_UPLOAD_MAX_MEMORY_SIZE, the data of files will be streamed to FILE_UPLOAD_TEMP_DIR https://docs.djangoproject.com/en/2.2/ref/settings/#file-upload-temp-dir

I don't think you have to set FILE_UPLOAD_MAX_MEMORY_SIZE, because it's the size of memory cache.

Upvotes: 3

Sreevardhan Reddy
Sreevardhan Reddy

Reputation: 731

FILE_UPLOAD_MAX_MEMORY will load the file contents into the RAM, if the request body or the file which is uploaded is more than FILE_UPLOAD_MAX_MEMORY then the file will be stored in the /tmp directory.

You can restrict the file size in the webserver (Nginx) by adding client_max_body_size 20M;

Here 20 megabytes is the maximum data that request body can have. So if you are uploading a file more than 20 MB, the webserver will not accept it.

Upvotes: 2

Related Questions