【发布时间】:2020-12-10 22:35:47
【问题描述】:
我正在尝试使用 pathfs 检测存储在 mongodb 数据库中的文件的 mimetype。我正在使用“mimetypes”来执行此操作,我尝试将文件作为字节传递,但收到错误 TypeError: cannot use a string pattern on a bytes-like object
我的代码:
location = cur_post['file']
file_mimetype = mimetypes.guess_type(fs.get(location).read())
print(file_mimetype)
return send_file(BytesIO(fs.get(location).read()), mimetype=file_mimetype, as_attachment=False, attachment_filename=(str(random.randint(10000000,99999999)) + '.pdf'))
我也试过mimetypes.guess_type(io.BytesIO(fs.get(location).read())),也遇到了类似的错误
【问题讨论】:
-
mimetypes使用文件名从文件扩展名中猜测 mime 类型,而不是通过从文件内容中读取魔术字节。使用不同的库,例如 python-magic。
标签: python mime-types