python - 如何检查上传到服务器的文件的 mimetype?

标签 python django linux file unix

这只是一个用户上传一个文件。

最佳答案

UploadedFile.content_type 将返回上传时随文件一起发送的内容类型 header 。

如果您还需要在保存后检查文件,您可以使用 python 中的 mimetypes 模块。但它似乎只根据文件扩展名进行检查。

import mimetypes
file_type, file_encoding = mimetypes.guess_type('/path/to/file')
print 'File-type: %s\nFile-encoding: %s' % (file_type, file_encoding)

如果您有默认情况下未检测到的文件类型要求,您也可以在使用 guess_type 之前将类型简单地添加到 mimetypes:

mimetypes.add_type('font/ttf', '.ttf')

关于python - 如何检查上传到服务器的文件的 mimetype?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170637/

相关文章:

django - Django如何重命名上传的文件?

linux - 我如何使用私有(private)安装的 wxWidgets

linux - 如何要求用户输入,这应该是必需的

Python Openpyxl ...删除具有特定单词的行

python - 在 Linux 上交叉编译 Python 3.5.2 for ARM 时如何编译缺失的扩展模块

python - 添加时间戳和用户名到日志

php - 类似于 PHP 中的 python-DJANGO 的东西?

windows - CentOS 与 Windows Server 2008

python - 选择 pandas DataFrame 中的列子集与元组匹配的位置

python - 如何使用 lxml 针对多个 xsd 模式进行验证?