This question has been flagged
1 Reply
6502 Views

Hello.

I have an API that returns file content as base64 encoded bites. I need to let user download or preview the file (best would be both). My idea was to put base64 bites to binary field and in the view use binary widget. However the binary field does not understand the mime type of the file, thus making the mime type to octet-stream and then not representing it correctly. 

Is there a way to provide mime type to binary field (API returns the mime type of the file), so it would understand what kind of file it is?

What other way I can show/download the file from the API in odoo temporary?

P.S. I cannot save the file in odoo system because of security reasons and not to duplicate files.

Thank you for your answers in advance.

Avatar
Discard
Best Answer

Unfortunately, the Binary field does not allow to specify the mimetype as far as I know.

However, there are workarounds. You can create the attachment manually and set it: env['ir.attachment'].create with mime_type. Instead of a Binary field, you can use a relation to the attachment.

Sometimes Odoo computes this attachment relation, for example in addons/delivery/models/stock_picking.py#_compute_return_label . In the view, it uses a link to /web/content/{id}

Another possibility is to create a custom route for your file where you set the Content-Type accordingly.

Avatar
Discard