Search code examples
djangopython-3.xfile-uploadtemporary-filesbytesio

_io.BytesIO' object has no attribute 'name' for small size file


I'm uploading file with python/Django and getting two different object. When file is small in size, getting InMemoryUploadedFile object, while file is quite large, i got in temporaryFileWrapper. I m checking file mime type with magic library.

when File is large, getting correctmime type with this code

file_name = self.cleaned_data.get('file')
file_mime= magic.from_file(file_name.file.name, mime=True)

supported_format= ['video/x-flv','video/mp4','video/3gpp','video/x-ms-wmv']
 if file_mime  in supported_format:
    ...........

But when file is small in size i m getting error

_io.BytesIO' object has no attribute 'name

For large file

enter image description here

For small file

enter image description here


Solution

  • As per Django's this doc, Django have two upload file handler MemoryFileUploadHandler and TemporaryFileUploadHandler. MemoryFileUploadHandler stream to memory , and TemporaryFileUploadHandler stream to disk.

    I have set deafult TemporaryFileUploadHandler im my setting.py

    FILE_UPLOAD_HANDLERS= ["django.core.files.uploadhandler.TemporaryFileUploadHandler"]
    

    We can also write own custom FileUploadHandler in django