Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
2 Besvarelser
271 Visninger

Hi,


I'm new here ant testing external API to create account bills.


I've created with success an account_move record, and account_movelines records 


But, when I upload a pdf file to ir_attachment, this file not appear in odoo site. I got an "file can't be loaded" error in site. I converted file binary in base64 string.


My odoo version is 18, and it onsite (instead local installation). I noted that odoo 18 default configuration is save files on local store, instead sabe in database.


So, what can I do to upload files and see them in site?



Thank you!

Avatar
Kassér
Forfatter Bedste svar

Got it, and discovered my problem.


i'm using the column "db_datas" instead of "datas".


"db_datas" stores all the base64 into database, and "datas" stores base64 in filestore

Thanks a lot!

Avatar
Kassér
Bedste svar

Hi,


Please try it like this.


import xmlrpc.client

import base64


common = xmlrpc.client.ServerProxy('http://localhost:9016/xmlrpc/2/common')

uid = common.authenticate('dbname', 'username', 'key', {})

models = xmlrpc.client.ServerProxy('http://localhost:9016/xmlrpc/2/object')



with open("test.pdf", "rb") as f:

    encoded_file = base64.b64encode(f.read()).decode("utf-8")


attachment_id = models.execute_kw(

    'dbname', 'username', 'key',

    'ir.attachment', 'create',

    [{

        'name': 'Sample Attachment',

        'type': 'binary',

        'datas': encoded_file,

        'res_model': 'sale.order', <--- your model

        'res_id': 324324,  # <--- your line ID

        'mimetype': 'application/pdf',

        'store_fname': 'test.pdf',

        'public': True, # <----- optional for public access

    }]

)

print("Attachment created with ID:", attachment_id)



Now to ensure this attachment is created you can check by going to url '/web/content/{attachment_id}.


Hope it helps

Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
1
jun. 25
1902
1
jul. 25
890
1
apr. 25
1545
2
jan. 25
1726
1
jun. 24
2549