Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to create a file on the server, and download it on the client?

By
Eric
on 7/1/14, 5:14 PM 1,824 views

Hello, I need to output a file on the server so that users can download it on their machines.  How can I do this in Odoo?

1

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 7/2/14, 4:55 AM

Probably the best solution is to create it server side, base64 encode it, and attach it to relevant document... 
For simplicity here is example.. 
Let's say you want some xml report... 
1. you generate the xml_string then you pass it to a method like this:

def _attach_xml_file(self, cr, uid, ids, xml_string, context=None):  

      import base64

        assert len(ids) == 1, "Only one ID accepted"
        model_obj= self.pool.get('your.model')          # the model of record you want file attached
        record_obj= model_obj.browse(cr, uid, ids[0]) # actual record to wich you attach file
        file_name = 'name_your_file_here.xml'
        attach_name = file_name
        attach_obj = self.pool.get('ir.attachment')
        context.update({'default_res_id': ids[0], 
                                 'default_res_model': 'your.model'})

        attach_id = attach_obj.create(cr, uid, {'name': attach_name, 
                                                'datas': base64.encodestring(xml_string), 
                                                'datas_fname': file_name}, context=context)
        return attach_id

2. now yout file is attached to "record_obj" of "your.model"
Example: you need some additional conditions in xml form attached to sale order  then your.model = sale.order, and record_obj is the id of selected order... 
3. File is accesible for download to client via Attachment button (top of order view)

 

hope it helps...

Thank you for your reply. In my case I wanted to stay away from the ir.attachments model, but all i needed to do was read the file I created into a string variable. Then pass the string into the base64.encodestring(string_variable). Now I am able to download the file from the new model that I created. Thank you for your help

Eric
on 7/2/14, 12:59 PM
1

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 7/2/14, 4:55 AM

Probably the best solution is to create it server side, base64 encode it, and attach it to relevant document... 
For simplicity here is example.. 
Let's say you want some xml report... 
1. you generate the xml_string then you pass it to a method like this:

def _attach_xml_file(self, cr, uid, ids, xml_string, context=None):

 import base64

        assert len(ids) == 1, "Only one ID accepted"
        model_obj= self.pool.get('your.model')          # the model of record you want file attached
        record_obj= model_obj.browse(cr, uid, ids[0]) # actual record to wich you attach file
        file_name = 'name_your_file_here.xml'
        attach_name = file_name
        attach_obj = self.pool.get('ir.attachment')
        context.update({'default_res_id': ids[0], 
                                 'default_res_model': 'your.model'})

        attach_id = attach_obj.create(cr, uid, {'name': attach_name, 
                                                'datas': base64.encodestring(xml_string), 
                                                'datas_fname': file_name}, context=context)
        return attach_id

0
Eric
On 7/2/14, 7:26 PM

First I added these two lines in the top of my python file

import io # used to output a file with exported data

import base64 # used to encode the contents of the exported file in binary form. this is used to download the file once its created

 

Fields Added in _columns { } on the python file:

"file_name": fields.char( "File Name" )

"file_binary": fields.binary( "Binary File" )

 

Code written in create()

I used the create method to save the new record with the binary data

def create(self, cr, uid, vals, context=None):

    # code goes in here...

 

Create the file:

Then I created the file like this in write mode

file_obj = open( "c:\Users\name_of_file.txt", "w")

file_obj.write( "some sample text..." )

file_obj.close()

 

Read file and encode with base64

# Re open the file_obj in read mode

file_obj = open( "c:\Users\name_of_file.txt", "r")

file_string = file_obj.read()

vals[ "file_binary" ] = base64.encodestring( file_string )   # Assuming this is in the create() and using the vals{ } to save the data

file_obj.close()

 

Create record with the filename and binary file data

new_id = super(current_model_used, self).create(cr, uid, vals, context)

 

XML form and tree views

Add the new fields on the form and tree views like this. This way when you download the file, then the file name will be used.

< field name="file_name" />

< field name="file_binary" filename="file_name" /> <!-- notice the filename attribute. Use this to set the file name with "file_name"  field-->

 

The end. Hope this helps!

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/1/14, 5:14 PM
Seen: 1824 times
Last updated: 3/16/15, 8:10 AM