This question has been flagged
9 Replies
6977 Views

Hello. How can I send the file, what I have on the disk, to my browser to download, like "save as? Or how can I do that when I've finished to compose my csv? Thanks.

Avatar
Discard

Do you have the solution, I am looking for it too.

Hello.
I used a binary field for that. It's not what I looked for, but I have only this solution. The question is not actual for me now, but I think there is another way.

--
Отправлено из Mail.Ru для Android

понедельник, 21 декабря 2015г., 09:44 +06:00 от JC jcchua-gmail.com@mail.odoo.com>:


Do you have the solution, I am looking for it too.

--
JC

Best Answer

You can use URL-Action to send your file, but before that, you need to dump the data into IRAttachment, then use this attachment to download the file via Browser(URL)

A sample snippet as follows: (in my case am writing into an Excel File & returning the same as downloaded)

# Attach & Print: Excel File 
out = base64.encodestring(result_ExcelFile)
 
attachment = self.env['ir.attachment'].create({
'name': <file_name>,
'datas': out,
'datas_fname': <file_name>,
'res_model': self._name,
'res_id': self.id,
'type': 'binary'})
 
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
 
return {'type' : 'ir.actions.act_url',
'url': base_url + '/web/content/%s/%s'%(attachment.id, <file_name>),
'target': 'self',
}


Avatar
Discard
Best Answer

Hi,

"saveas" or "saveas_ajax" python functions are in module web/controllers/main.py in class Binary (but I think you should create yours using what you need in saveas method, because this function use attachment)

(v8) : def saveas(self, model, field, id=None, filename_field=None, **kw):

bye

Avatar
Discard
Author

Thank you for your answer. I know everything you're speaking about, but maybe you didn't understand what I want to get as result. I have the module, where I make my own .csv file and after making process I need to download it via my browser. How can I do that? How can I send it to browser?

Sorry, it's not very clear => I have the module, where I make my own .csv file (you edit it in odoo ?), => after making process (what process); you should explain all actions you are doing, where ... bye

Author

In the module I make new csv file? like this out = csv.writer(open("new_file.csv"), delimiter=','). And then I write to this file everything I want. And as result, I want to download the file, new_file.csv, via my browser.

answer updated

Best Answer

you can use binary field I think, add a binary field to your model and when you make new csv file, save this file to the binary field. you'll be able then to download it (normally with binary field you'll have upload too.. but if you try to set this field to readonly, then maybe upload will be disabled(?))

Avatar
Discard