@http.route('/fmcg/download/pdf', type='http', auth="public", csrf=False, cors='*')
def upload_download_pdf(self, **kw):
"""下载pdf接口:直接返回的是一个文件对象,可直接下载
"""
report = request.env.ref('zimo_fmcg_app.action_inventory_report').with_user(SUPERUSER_ID)._render_qweb_pdf(int(kw('id')))
# pdf_doc = request.env['ir.attachment'].sudo().search([('id', '=', 726)], limit=1)
# data = io.BytesIO(base64.standard_b64decode(pdf_doc.datas))
data = io.BytesIO(report[0])
return http.send_file(data, filename='melon_inv.pdf', as_attachment=True)
@http.route('/fmcg/download/pdf2', type='json', methods=['POST'], auth="public", csrf=False, cors='*')
def upload_download_pdf2(self, **kw):
"""下载pdf接口 todo 先用固定的pdf文件测试
model值:
zimo.store.inventory
zimo.store.transaction
传参
{"id":"",
"model":"zimo.store.inventory"
}
"""
# data = request.jsonrequest
# report = request.env.ref('zimo_fmcg_app.action_inventory_report').with_user(SUPERUSER_ID)._render_qweb_pdf(1)
# filename = 'test' + '.pdf'
# attachment = request.env['ir.attachment'].sudo().create({
# 'name': filename,
# 'type': 'binary',
# 'datas': base64.b64encode(report[0]),
# 'res_model': 'zimo.store.inventory',
# 'res_id': 1,
# 'mimetype': 'application/x-pdf'
# })
pdf_attachment = request.env['ir.attachment'].sudo().search([('id', '=', 3292)], limit=1)
return json.dumps(
{"data": [{"pdf_file": "https://www.hxmelon.com/ad/content/%s-%s" % (pdf_attachment.id, pdf_attachment.checksum)}],
"result": "success"})
二、图片传送接口
有两种方式:
1、一种是传对象
base64_data = base64.b64encode(kw['file'].read())
2、一种是传文件内容
file = request.httprequest.data
@http.route('/fmcg/active/image', type='http', methods=['POST'], auth="public", csrf=False, cors='*')
def upload_active_imaget(self, **kw):
"""上传图片接口
"""
file = request.httprequest.data
_logger.info("-----------/fmcg/active/image--------------------------:%s", kw['file'])
if not kw['file']:
return json.dumps({'result': 'success', 'message': 'no image'})
# base64_data = base64.b64encode(kw['image_file'].read())
# base64_data = False
base64_data = base64.b64encode(kw['file'].read())
active_obj = request.env['zimo.store.activities'].sudo()
s_id = active_obj.search([('name', '=', kw['sid'])], limit=1)
if not s_id:
return json.dumps({'result': 'fail', 'message': 'not find record'})
attachment = request.env['ir.attachment'].sudo().create({
'name': kw['file'].filename,
'datas': base64_data,
'res_id': s_id.id,
'res_model': 'zimo.store.activities',
})
s_id.write({'attach_ids': [(6, 0, [attachment.id])]})
return json.dumps({'result': 'success', 'message': 'success'})
心有猛虎,细嗅蔷薇