This question has been flagged

all the data printed in the text file are in one line:

The text file was saved as binary file and downloaded.


Sample output:

MarkKim

instead of 

Mark

Kim

But when i'm printing what's inside the textfile it seems fine.. Is there something wrong during encoding? or do I need to add more.. I've been trying this so many times please help. thank you.

CODE:

     textfile = open('Name.txt', 'w')

     textfile.write('%s' % name + "\n")

        with open('Name.txt', 'r') as f_read:

            file_data = f_read.read()

            # print 'file_data', file_data

            data_encode = file_data.encode('utf8').encode('base64')


        values = {

            'name': 'Name.txt',

            'datas_fname': 'Name.txt',

            'res_model': 'ir.ui.view',

            'res_id': False,

            'type': 'binary',

            'public': True,

            'datas': data_encode,


        }


        attachment_id = self.env['ir.attachment'].sudo().create(values)


        # Prepare your download URL

        download_url = '/web/content/' + str(attachment_id.id) + '?download=True'

        base_url = self.env['ir.config_parameter'].get_param('web.base.url')


        return {

            "type": "ir.actions.act_url",

            "url": str(base_url) + str(download_url),

            "target": "new",

        }



Avatar
Discard

windows - linux - mac treats newlines separately use \r\n instead of \n

Author

Thank you so much @F.P. it worked! :)