Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
3449 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer
Auteur

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

Auteur

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

Gerelateerde posts Antwoorden Weergaven Activiteit
1
mrt. 15
4779
3
aug. 24
1473
3
dec. 23
1983
2
mrt. 21
3617
1
mei 17
3086