2

我想通过以下方式在上传文件之前检查文件类型:

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

当我上传图片时,我收到一个错误:

'NoneType' object has no attribute 'content_type'

这里有什么问题?

4

1 回答 1

7

imghdr.what如果图像有效,则将图像格式作为字符串(gif、png 等)返回,None否则。

用法:

import imghdr
imghdr.what(value)

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

于 2013-04-27T13:31:29.983 回答