Last active
March 2, 2022 11:23
-
-
Save PierreAlainKouakou/29f8e52b223dfc2bf4331442de187be5 to your computer and use it in GitHub Desktop.
How to download any binary file on click from Odoo website?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from odoo import _ | |
from odoo import http | |
from odoo.http import Controller, request, route | |
class OnlineRequests(Controller): | |
@http.route('/web/download/binary/<string:model>/<int:res_id>/<string:field>/<string:filename>', type='http', auth="public") | |
def download_document(self, model, field, res_id, filename=None, **kw): | |
response = None | |
status, headers, content = request.env['ir.http'].sudo().binary_content( | |
model=model, | |
id=res_id, | |
field=field, | |
default_mimetype='application/octet-stream', | |
) | |
if status == 200: | |
content_base64 = base64.b64decode(content) | |
headers.append(('Content-Length', len(content_base64))) | |
response = request.make_response(content_base64, headers) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment