Skip to Content
Menu
This question has been flagged

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

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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

Author

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 Replies Views Activity
1
Mar 15
4164
3
Aug 24
652
3
Dec 23
1120
2
Mar 21
3101
1
May 17
2562