콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
3730 화면

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

아바타
취소
베스트 답변

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

아바타
취소
작성자

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

작성자

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

관련 게시물 답글 화면 활동
1
3월 15
4911
3
8월 24
1693
3
12월 23
2401
2
3월 21
3806
1
5월 17
3222