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

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.

아바타
취소

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.

베스트 답변
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.
아바타
취소
작성자

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'?

작성자

It already worked. thank you so much.

작성자

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.

작성자

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.

작성자

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

베스트 답변

Aktiv Softwares solution worked for me with one twist: 

import base64

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


아바타
취소
관련 게시물 답글 화면 활동
0
3월 15
4338
0
9월 17
5431
1
4월 22
2186
2
11월 15
7078
1
3월 15
6467