Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max Image Upload size Validation Django REST API

I am using Django and Django Rest Framework. How to validate the image size of the image posted to django rest framework api ?

This is a bit confusing :

FILE_UPLOAD_MAX_MEMORY_SIZE = #something

The maximum size, in bytes, for files that will be uploaded into memory. Files larger than FILE_UPLOAD_MAX_MEMORY_SIZE will be streamed to disk. Defaults to 2.5 megabytes.

I set this to 255 and even then I was able to upload a 2MB image from django admin. I want to reject the POST request and raise error if image size is larger than 5MB

like image 647
Ashish Gupta Avatar asked Aug 13 '15 06:08

Ashish Gupta


People also ask

How do I limit upload size in Django?

Create a file named formatChecker.py inside the app where the you have the model that has the FileField that you want to accept a certain file type. Then instead of using 'FileField', use this 'ContentTypeRestrictedFileField'. You can change the value of 'max_upload_size' to the limit of file size that you want.

What is Restapi in Django?

REST APIs are an industry-standard way for web services to send and receive data. They use HTTP request methods to facilitate the request-response cycle and typically transfer data using JSON, and more rarely - HTML, XML and other formats.


1 Answers

FILE_UPLOAD_MAX_MEMORY_SIZE defines the maximum size a file can reach in memory before being streamed to disk. This is an optimization for uploading files. It does not limit the maximum file size that can be uploaded.

While this doesn't answer your question I hope it clears up your confusion about the setting.

like image 148
Matt Avatar answered Oct 21 '22 13:10

Matt