【问题标题】:How to get mimetype of a field binary in Odoo10?如何在 Odoo10 中获取字段二进制的 mimetype?
【发布时间】:2020-05-28 07:14:46
【问题描述】:

我创建了一个名为datas 的二进制字段并上传了一个文件。我需要获取数据的mimetype

我试过了。

*.py

  attachment_icon = fields.Char(string="Icon", compute="_get_icon")

    @api.one
    def _get_icon(self):
        file = None
        for rec in self:
            print('data',type(rec.datas)) //it print type<str>
            binary_data = rec.datas
            print('binary_data',binary_data)
            mimetype = guess_mimetype(binary_data.encode('base 64'))
            print('mimetypemimetype',mimetype)// print 'text/plain'

现在mimetype的输出是text/plain,实际上上传的文件是pdf。 如何获得正确的 mimetype?

【问题讨论】:

  • 你可以试试libmagic.from_buffer,它接受二进制字符串并返回检测到的文件类型。

标签: python-2.7 odoo odoo-10


【解决方案1】:

你好@KbiR

Python 的魔法函数会得到 mimetype:

import magic
mime = magic.Magic(mime=True)
mime.from_file("youtPath/fileName.pdf") # 'application/pdf'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2018-01-08
    • 2018-02-14
    • 2015-06-07
    • 2015-01-28
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多