odoo16 Controller文件上传
服务端
1 import logging 2 import json 3 from base64 import b64encode 4 from odoo import http, SUPERUSER_ID, _ 5 from odoo.http import request 6 7 _logger = logging.getLogger(__name__) 8 9 class Main(http.Controller): 10 11 @http.route('/douban/film', auth='none', type='http', csrf=False) 12 def douban_film(self, **post): 13 indata = { 14 'name':post.get('name'), 15 'detail_url': post.get('detail_url'), 16 'info': post.get('info'), 17 'theme': b64encode(post.get('theme').read()) 18 } 19 request.env['douban.film'].sudo().create(indata) 20 21 rstr = json.dumps({'ss':0}, sort_keys=False, indent=4, separators=(',', ':'), ensure_ascii=False) 22 return rstr
Python客户端
1 import requests 2 def main(): 3 data = { 4 'name': 'test', 5 'detail_url': 'detail_url', 6 'info': 'info', 7 # 'theme': open('lll.png', 'rb') 8 } 9 10 r = requests.post('http://localhost:8069/douban/film', data=data, files={'theme': open('lll.png', 'rb')}) 11 print(r.text) 12 13 if __name__ == '__main__': 14 main()