Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
9 Replies
11569 Tampilan

wanted to export a txt file but whenever I made one it is downloaded in the server.

Is it possible to print/download it like qweb report where it is saved in the /downloads of the pc you're using.

Avatar
Buang

I think this should be possible in the following way: you save the file on your server and generate a new http route that will send the file to the user. See: https://www.odoo.com/documentation/9.0/reference/http.html (the v10 documentation is pretty scarce). There might be a better way though and I have never done something like that, so no guarantees, that it will work.

Jawaban Terbai
Hello, 

You can try below code for downloading your file as odoo default report (sale order report)

f_read = Your test file # Test file 
file_data = f_read.read()
#Pass your text file data using encoding.
values = {
            'name': "Name of text file.txt",
            'datas_fname': 'print_file_name.txt',
            'res_model': 'ir.ui.view',
            'res_id': False,
            'type': 'binary',
            'public': True,
            'datas': file_data.encode('utf8').encode('base64'),
        }
Using your data create as attachment
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 so it will download in your system
return {
        "type": "ir.actions.act_url",
        "url": str(base_url)  +  str(download_url),
        "target": "new",
     }

Hope this will help you!
Thanks.
Avatar
Buang
Penulis

i've tested your code, but whenever i tried to print whats inside 'file_data', it prints nothing. does it mean that theres no data passed in 'file_data'?

Penulis

It already worked. thank you so much.

Penulis

Now I'm trying to put multiple rows in my txt file. but instead of making a new line, it's just in one line/row. what should I do? is the line: 'datas': file_data.encode('utf8').encode('base64'),

gave that result? I used \n and \t, I tried printing the file_data and it has the right output but in the txt file downloaded it's in one line only. thank you so much for helping me.

Are you trying write using python code then you can put code like this,

f_read.write("\tTest line\n") It will evaluate "\n",

And if you are using exisiting ".text" file then no need to write "\n" simply put your lines as per your format.

After encoding, you can see your file content using decode as below.

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

data_decode = data_encode.decode('base64','strict').

Hope this will help you.

Penulis

Hello.. should I put it in the values {} ?

I don't know where I should put

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

data_decode = data_encode.decode('base64','strict')

thanks in advance.

Penulis

Hello, I still have the same problem and when I tried that one it shows 'Incorrect padding' error.

Jawaban Terbai

Aktiv Softwares solution worked for me with one twist: 

import base64

...
base64.b64encode(file_data.encode('utf8'))


Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
0
Mar 15
4234
0
Sep 17
5312
1
Apr 22
2116
2
Nov 15
6933
1
Mar 15
6343