تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
6452 أدوات العرض

Hello,

I am struggling to add an attachment to a mail message that I create through the XMLRPC API for Odoo 14.

I get the error message...  argument should be a bytes-like object or ASCII string, not \'Binary\'

But when I print the type of my argument, it says class 'bytes'

What am I missing ?

Code follows...

with open("c:/users/lwolf/pictures/WinUpdate1.png", "rb") as image_file:
  imageBase64 = base64.b64encode(image_file.read())

print("The type of imageBase64 is ", type(imageBase64))

models.execute_kw(db, uid, password,
    'ir.attachment',
    'create',
             [{
                'res_model' : 'mail.message',
                'name' : 'WinUpdate Image',
                'res_id' : id,
                'type' : 'binary',
                'mimetype' : 'image/png',
                'store_fname' : 'WinUpdate1.png',
                'datas' : imageBase64
                }]
            )


الصورة الرمزية
إهمال

Hello Leonard, i am facing similar problem. have you found a fix?

الكاتب

Hello Bogdan,
Sorry to say, I have not been able to resolve this. I've given up for now until someone can give me advice.

أفضل إجابة
Please check this may be help
with open("c:/users/lwolf/pictures/WinUpdate1.png", "rb") as image_file:
  data = base64.b64encode(image_file.read())
imageBase64 = Image.open(io.BytesIO(data))

models.execute_kw(db, uid, password,
    'ir.attachment',
    'create',
             [{
                'res_model' : 'mail.message',
                'name' : 'WinUpdate Image',
                'res_id' : id,
                'type' : 'binary',
                'mimetype' : 'image/png',
                'store_fname' : 'WinUpdate1.png',                                
                'datas': base64.b64encode(imageBase64)
                }]           
)
الصورة الرمزية
إهمال

How do you get the id field in 'res_id' : id ?
Thanks

أفضل إجابة

Hello all, 

The datas you send through the 'datas' key must be a "stringify" of your binary.

Instead of sending  

b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs="
You should sending :
R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=

You can obtain it by using : imageBase64.decode()

Hope this will help you.

الصورة الرمزية
إهمال
أفضل إجابة

convert to string

str(imageBase64)[2:]


الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
3
يوليو 25
706
1
سبتمبر 23
2290
1
أغسطس 23
13138
3
يناير 23
7540
1
مارس 22
5398