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

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!

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

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!

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

Hi, please check this: https://youtu.be/Cmbo2iGuTBY 
Hope it helps.

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

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 8 25
356
2
thg 7 25
2803
1
thg 6 25
2072
1
thg 8 25
990
1
thg 4 25
1631