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

I'm trying to download a text file but when I click the button, it respondes with a 404 error, what am i doing wrong?

from odoo import models, fields

import base64
class HrEmployee(models.Model):   

     _inherit = "hr.employee"
    your_file = fields.Binary("My_file.txt")


    defaction_test_function(self):               

        data = "Your text goes here"        

        self.your_file = base64.b64encode(data.encode())
        return {            

            'type': 'ir.actions.act_url',           

             'url': '/web/content/hr_employee/%s/custom_addon/models/%s?download=true' % (self.id, "My_File.txt"),       

            }




thanks in advice, every help is appreciated

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

Hi,

You can try this following code

attachment_id = fields.Many2one('ir.attachment', string='Attachment', help='Attachment')
def download_attachment(self):
    return {
        'type': 'ir.actions.act_url',
        'url': '/web/image?model=ir.attachment&field=datas&id=%s&filename=%s' % (self.attachment_id.id, self.attachment_id.name),
    }

Regards

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

this attachment results empty, how can I put my text file on it?

Tác giả

I solved with this code:

self.attachment_id = self.env['ir.attachment'].create({
'name': 'file.txt',
'type': 'binary',
'datas': base64.b64encode(data.encode()),
'res_id': self.id,
'mimetype': 'text/plain'
})

now the url works, thank you, really appreciated

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 3 15
4999
3
thg 8 24
1860
3
thg 12 23
2519
2
thg 3 21
3907
1
thg 5 17
3297