图像文件的Django内容类型

hln*_*hln 2 python django image identification imghdr

我想在上传之前检查文件类型:

content = self.cleaned_data['picture']
content_type = content.content_type.split('/')[0]
Run Code Online (Sandbox Code Playgroud)

上传图片时出现错误:

'NoneType' object has no attribute 'content_type'
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

vau*_*tah 5

imghdr.what如果图片有效,则将以字符串(gif,png等)的形式返回图片格式None。

用法:

import imghdr
imghdr.what(value)
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,value可以是文件(类)对象或文件名。

  • 有用的未知内置于我 (3认同)