odoo使用Binary字段实现点击按钮下载文件功能

8 篇文章 0 订阅

1、定义bianry字段和下载文件名/按钮名字段

    download_button_test = fields.Binary(compute='_compute_download_button_test', string='下载委托测试报告')
    download_button_check = fields.Binary(compute='_compute_download_button_check', string='下载委托检验报告')
    download_button_check_name = fields.Char(string="委托检验报告名", default='打印委托检验报告')
    download_button_test_name = fields.Char(string="委托测试报告名", default='打印委托测试报告')

2、实现binary字段的计算方法,这里是下载的附件

content_path = os.path.join(res_pdf._filestore(), res_pdf.store_fname)
是为了获取附件的真实路径
order.download_button_check = base64.b64encode(open(content_path, ‘rb’).read())
将该附件打开读取后编码再复制给binary字段即可成功
注:这里可以下载任何文件,只需要将要下载的文件的路径赋值给content_path 即可。

    @api.model
    def _compute_download_button_check(self):
        for order in self:
            if order.state in ['pz','dy','ff']:
                order.download_button_check_name = order.name + '委托检验报告.pdf'
                res_pdf = self.env['ir.attachment'].search([('res_model', '=', 'lims.compile.report'), ('res_id', '=', order.id), ('description', '=', '[%s]委托检验报告' % (order.name))])
                if res_pdf:
                    res_pdf = res_pdf[0]
                else:
                    order.download_button_check = False
                    return False
                content_path = os.path.join(res_pdf._filestore(), res_pdf.store_fname)

                # content_path = get_module_resource('lims_report', 'static/src/report_pdf', order.name+'委托检验报告.pdf')
                if content_path:
                    order.download_button_check = base64.b64encode(open(content_path, 'rb').read())
                else:
                    order.download_button_check = False
            else:
                order.download_button_check = False

3、在xml视图中如此写入即可实现该功能

<field name="download_button_check" filename="download_button_check_name" class="btn-primary" style="color: white;height: 25px;" string='下载'/>
<field name="download_button_check_name" invisible="1"/>
<field name="download_button_test" filename="download_button_test_name" class="btn-primary" style="color: white;height: 25px;margin-left:3px" string='下载'/>
<field name="download_button_test_name" invisible="1"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值