python 检查文件mime类型的方法

magic 模块可以检查文件的mime类型,而不是从后缀名来判断,例如判断文件是不是视频或图片类型如下:

#检查文件类型

mime_type = magic.from_file(full_path,mime=True)

logger.info("上传的文件类型:"+str(mime_type))

if not mime_type.startswith('video') and not mime_type.startswith('image'):

logger.error("非法的文件类型!")

os.remove(full_path)

return JsonResponse({'code':500,'msg':'非法的文件类型!'})

以上这篇python 检查文件mime类型的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

以上是 python 检查文件mime类型的方法 的全部内容, 来源链接: utcz.com/z/318830.html

回到顶部