Skip to content

Instantly share code, notes, and snippets.

@ruter
Last active December 19, 2018 11:23
Show Gist options
  • Save ruter/d1aa8a2ec0270e4af202fb2a0d7d7a98 to your computer and use it in GitHub Desktop.
Save ruter/d1aa8a2ec0270e4af202fb2a0d7d7a98 to your computer and use it in GitHub Desktop.
[上传文件 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'非法请求!'
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment