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
_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