Skip to Content
Menu
This question has been flagged
2 Replies
5841 Views

I would like to get the binary data from a file that I uploaded into a Binary Field. 

This is what I tried but when you print out file it is just a file size and when this executes it 

throws > /usr/lib/python3.8/base64.py(87)b64decode()
-> return binascii.a2b_base64(s)


file = fields.Binary(string="Video File", store=True, attachment=False)
f_name = fields.Char(strng="Video File Name")
binary_raw = fields.Char(string="binary Raw", compute='_get_binary', store=False)

def _get_binary(self):
    print("here",self.id)
    file = base64.b64decode(self.file)


Thanks in advance for you help 



Avatar
Discard
Author

_name = 'gjlcrm.marketing_audio'Here is the solution thanks Jainesh for all your help in this matter it lead me to the answer
_name = 'gjlcrm.marketing_audio'
_descriptions = 'stores audio for marketing Movies'
name = fields.Char(string="Audio Name")
file = fields.Binary(string="Audio File", attachment=True, store=True)
f_name = fields.Char(strng="Audio File Name")
binary_raw = fields.Text(string="File Binary", compute='_get_binary', store=False)

def _get_binary(self):
for rec in self:
file = False
binary = self.env["ir.attachment"].sudo().search([("res_model", "=", "gjlcrm.marketing_audio"),("res_id", "=", rec.id),("res_field", "=", "file"),],limit=1)
print("Binary", binary)
if binary:
path = binary.store_fname
print(path)
file_datas = binary._file_read(fname=binary.store_fname)
print('file_datas', file_datas)
rec.binary_raw = file_datas
_descriptions = 'stores audio for marketing Movies'
name = fields.Char(string="Audio Name")
file = fields.Binary(string="Audio File", store=True, attachment=True)
f_name = fields.Char(strng="Audio File Name")
binary_raw = fields.Char(string="File Binary", compute='_get_binary', store=False)

def _get_binary(self):
    file = False
   
print(self.id)
    binary = self.env["ir.attachment"].sudo().search(
        [
            ("res_model", "=", "gjlcrm.marketing_audio"),
            ("res_id", "=", self.id),
            ("res_field", "=", "file"),
        ],
        limit=1,
    ).datas
    print(binary)
    if binary:
        file = base64.b64decode(binary)
    print("File: ", file)
    self.binary_raw = file

Here is the output and error I get... 

6
4.74 Mb
> /usr/lib/python3.8/base64.py(87)b64decode()
-> return binascii.a2b_base64(s)
(Pdb)


As you can see thinking that the binary returning 4.74 Mb causes the decode to not work ? Thanks again for the help 

Author Best Answer

Here is the solution thanks Jainesh for all your help in this matter it lead me to the answer
_name = 'gjlcrm.marketing_audio'
_descriptions = 'stores audio for marketing Movies'
name = fields.Char(string="Audio Name")
file = fields.Binary(string="Audio File", attachment=True, store=True)
f_name = fields.Char(strng="Audio File Name")
binary_raw = fields.Text(string="File Binary", compute='_get_binary', store=False)

def _get_binary(self):
for rec in self:
file = False
binary = self.env["ir.attachment"].sudo().search([("res_model", "=", "gjlcrm.marketing_audio"),("res_id", "=", rec.id),("res_field", "=", "file"),],limit=1)
print("Binary", binary)
if binary:
path = binary.store_fname
print(path)
file_datas = binary._file_read(fname=binary.store_fname)
print('file_datas', file_datas)
rec.binary_raw = file_datas

Avatar
Discard
Best Answer

Hello ron.kozlowski@easternauctions.com,

Please find code in comment.

I hope this will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

Here I’ve provided the example code to get binary data from text file and convert it to base64 string.

- Field Declaration:
file = fields.Binary(string="Video File", store=True, attachment=True)
f_name = fields.Char(string="Video File Name")
binary_raw = fields.Char(string="binary Raw", compute='_get_binary', store=False)

- Compute method:
def _get_binary(self):
file = False
binary = self.env["ir.attachment"].sudo().search(
[
("res_model", "=", "hr.employee"),
("res_id", "=", self.id),
("res_field", "=", "file"),
],
limit=1,
).datas
if binary:
file = base64.b64decode(binary)
print("File: ", file)
self.binary_raw = file

Related Posts Replies Views Activity
0
Jul 16
3624
1
Jan 18
4627
2
Sep 23
15269
1
Dec 22
4479
0
Jan 22
1632