Ruter
3/22/2018 - 1:10 PM

[上传文件 Controller] Odoo 上传文件 Controller #Odoo

[上传文件 Controller] Odoo 上传文件 Controller #Odoo

@http.route('/api/v1/mro/upload_file', type='http', auth="user", method=['POST'], website=True, csrf=False)
def upload_requirement_file(self, ufile, res_id=0, res_model, **kwargs):
    file = ufile
    if file:
        # 附件模型
        Attachment = request.env['ir.attachment']
        # 创建附件
        attach = Attachment.create({
            'name': file.filename,
            'res_model': res_model,
            'res_id': res_id,
            'datas': base64.encodestring(ufile.read()),
            'datas_fname': file.filename,
            'public': True
        })
        if attach:
            base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
            return request.make_response(json.dumps({
                'success': True,
                'msg': u'附件上传成功',
                'id': attach.id,
                'url': '{}/web/content?model=ir.attachment&field=datas&id={}&download=true&filename_field=datas_fname'.format(
                    base_url, attach.id),
                'local_url': attach.local_url
            }))
        else:
            return request.make_response(json.dumps({
                'success': True,
                'msg': u'附件上传失败'
            }))
    return request.make_response(json.dumps({
        'success': False,
        'msg': u'非法请求!'
    }))