Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
3819 Vizualizări

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

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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

Autor

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

Related Posts Răspunsuri Vizualizări Activitate
1
mar. 15
4959
3
aug. 24
1832
3
dec. 23
2490
2
mar. 21
3890
1
mai 17
3271