TypeError: cannot serialize '_io.BufferedRandom' object While trying to upload file in Django

Viewed 1076

I am trying to upload some files in Django but sometimes when the file size increases it gives the following error:

TypeError: cannot serialize '_io.BufferedRandom' object

In the current case I am trying to upload a file(size ~3Mb) but it's still showing the error. I tried to check in the network that if the file has been uploaded or not but it shows:

enter image description here

I am unable to understand what changes I need to make in order to resolve this

1 Answers

Try setting FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880 in settings.py to a higher value (5MB) so that files (<5MB) become InMemoryUploadedFile.

Default: 2621440 (i.e. 2.5 MB).

The maximum size (in bytes) that an upload will be before it gets streamed to the file system.

See Uploaded Files and Upload Handlers.

In the class MemoryFileUploadHandler(FileUploadHandler) it checks for the content length and if it's greater then it's use TemporaryFileUploadHandler:

def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
    """
    Use the content_length to signal whether or not this handler should be
    used.
    """
    # Check the content-length header to see if we should
    # If the post is too large, we cannot use the Memory handler.
    self.activated = content_length <= settings.FILE_UPLOAD_MAX_MEMORY_SIZE
Related