图像文件的Django内容类型 [英] Django content type of an image file

查看:76
本文介绍了图像文件的Django内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

上传图片时出现错误:

'NoneType' object has no attribute 'content_type'

这有什么问题吗?

推荐答案

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

用法:

import imghdr
imghdr.what(value)

在上面的示例中,value可以是类似文件的对象,也可以是文件名.

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

这篇关于图像文件的Django内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆