Django - Get uploaded file type / mimetype

前端 未结 4 1595
别那么骄傲
别那么骄傲 2020-12-13 15:26

Is there a way to get the content type of an upload file when overwriting the models save method? I have tried this:

def save(self):
    print(self.file.cont         


        
4条回答
  •  囚心锁ツ
    2020-12-13 15:42

    I'm using Django Rest Framework and this is the simplest I could go:

    file = request.data.get("file")    # type(file) = 'django.core.files.uploadedfile.InMemoryUploadedFile'
    print(file.content_type)
    

    Let's say I have uploaded a JPEG image then my output would be:

    image/jpeg
    

提交回复
热议问题