Django content type of an image file

泪湿孤枕 提交于 2019-12-11 03:43:45

问题


I want to check a file type before uploading it by:

content = self.cleaned_data['picture']
content_type = content.content_type.split('/')[0]

When I upload the picture I get an error:

'NoneType' object has no attribute 'content_type'

What can be wrong here?


回答1:


imghdr.what will return image format as string (gif, png etc.) if the image is valid, or None otherwise.

Usage:

import imghdr
imghdr.what(value)

In the above example value can be either a file(-like) object or a filename.



来源:https://stackoverflow.com/questions/16252656/django-content-type-of-an-image-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!