0

I'm developing with Odoo framework and when I get a binary file value from database I need to download it.

It's any module for doing it with python?

    @http.route(['/permissions/print/<int:permission_id>'], type='http', auth="public", website=True)
    def print_docr(self, permission_id=None):
        perm_id = request.env['res.partner.permission'].sudo().browse(permission_id)
        print(perm_id.attachment_doc)

This is the output of the file. enter image description here

Any suggestion? Thanks for reading!

arevilla009
  • 429
  • 3
  • 18
  • BytesIO might help you. I am not sure though... – Sabito stands with Ukraine Jun 26 '20 at 11:40
  • Your file is a PNG image, btw... https://stackoverflow.com/a/49690539/2836621 – Mark Setchell Jun 26 '20 at 12:25
  • Thanks the reply! It is any way to unconvert it into the original file and download it? @MarkSetchell – arevilla009 Jun 26 '20 at 12:30
  • 2
    I don't understand the question. If you know the file starts with `ivB0R`, you must have already downloaded it, in which case save it exactly as @g2i suggests - presumably with a `.png` extension. There appears to be no need to download it again nor to convert it to anything. – Mark Setchell Jun 26 '20 at 12:54
  • Check [press-a-button-and-download-a-file](https://www.odoo.com/fr_FR/forum/aide-1/question/press-a-button-and-download-a-file-how-93037) to see how to customize `print_docr` method to download `attachment_doc`. – Kenly Jun 26 '20 at 17:43

1 Answers1

0

I'm not sure I understood what you want, but it seems that you're downloading base64 strings to represent your binary files. If this is the case, you can firstly convert it to a bytes string:

from base64 import b64decode

bytes_string = b64decode(perm_id.attachment_doc)

Then writing it on a file:

with open("filename.bin", 'wb') as f:
    f.write(bytes_string)
Tristan Nemoz
  • 1,844
  • 1
  • 6
  • 19