Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
8906 Lượt xem

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 



Ảnh đại diện
Huỷ bỏ
Tác giả

_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 

Tác giả Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello ron.kozlowski@easternauctions.com,

Please find code in comment.

I hope this will help you.

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

Ảnh đại diện
Huỷ bỏ

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 7 16
4348
1
thg 1 18
5657
2
thg 9 23
17404
1
thg 12 22
5543
0
thg 1 22
2649