Odoo Help

1

Press a button and download a file, how?

By
E.M.
on 11/1/15, 2:21 PM 7,788 views

I would like to make Odoo download a file (from a text string for example) when a button is clicked. Is there any example I could start with or can anyone provide a basic code structure for downloading a file?

Thanks

6

Hi,

We at Emipro, got this question numerous times from different Odoo developers. So here I am giving the step by step guidance how to do this.

Here all my code is in V8.

  • Create a method inside your regular model and return URL. 

For example, 

 @api.multi 
def get_stock_file(self):
    return {
            'type' : 'ir.actions.act_url',
            'url': '/web/binary/download_document?model=wizard.product.stock.report&field=datas&id=%s&filename=product_stock.xls'%(self.id),
            'target': 'self',
}

Here I have returned URL with model and some info. Now in next step i will catch that URL to a controller method.

( just like file you can see in /web/controllers/main.py )

  • Make a controller class and catch that url and do process for download excel file.

from openerp import http
from openerp.http import request
from openerp.addons.web.controllers.main import serialize_exception,content_disposition
import base64
class Binary(http.Controller):
@http.route('/web/binary/download_document', type='http', auth="public")
@serialize_exception
def download_document(self,model,field,id,filename=None, **kw):
    """ Download link for files stored as binary fields.
    :param str model: name of the model to fetch the binary from
     :param str field: binary field
     :param str id: id of the record from which to fetch the binary
    :param str filename: field holding the file's name, if any
    :returns: :class:`werkzeug.wrappers.Response`
    """
    Model = request.registry[model]
    cr, uid, context = request.cr, request.uid, request.context
    fields = [field]
    res = Model.read(cr, uid, [int(id)], fields, context)[0]
    filecontent = base64.b64decode(res.get(field) or '')
    if not filecontent:
         return request.not_found()
    else:
        if not filename:
            filename = '%s_%s' % (model.replace('.', '_'), id)
            return request.make_response(filecontent,
                            [('Content-Type', 'application/octet-stream'),
                             ('Content-Disposition', content_disposition(filename))])

In above method I have got the ID from url and then applied some calculation and return the http response from request. Whatever values I have passed from wizard to controller method, I will get them on controller method.

( See below, I have passed model, field, id and filename from url )

/web/binary/download_document?model=wizard.product.stock.report&field=datas&id=%s&filename=product_stock.xls
def download_document(self,model,field,id,filename=None, **kw):

I have returned Excel file but you can return any kind of file and even an attachment from database binary field too.

By apply code like above, you will able to return any file on button click without intervention of binary field.

I hope this answer will add extra spice into your knowledge and make your Odoo technical understanding more delicious.

Odoo Technical notes is published for this. You can view How to download any file on button click ?  and review it.

This is super detailed and extremely useful. I have pending to try in my project as we need it to generate custom printing files for a thermal printer. Thanks a lot.

E.M.
on 11/8/15, 8:38 AM

Does this code work for odoo10 as well . I got error while using these codes.

res = Model.read(model,cr, uid, [int(id)], fields, context)[0]

TypeError: unbound method read() must be called with kyc.clients instance as first argument (got unicode instance instead)

Sarina
on 7/20/17, 3:03 AM
2
Akhil P Sivan
On 11/1/15, 4:48 PM

Hi,

You may try using a Binary field.

file = fields.Binary(string='File')
0
E.M.
On 1/27/16, 4:06 PM

This works fine but I do not fully understand this part of the code:

Model = request.registry[model]     
cr, uid, context = request.cr, request.uid, request.context     
fields = [field]
res = Model.read(cr, uid, [int(id)], fields, context)[0]

What does Model contain?

What does fields contain?

What does res contain?

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

4 follower(s)

Stats

Asked: 11/1/15, 2:21 PM
Seen: 7788 times
Last updated: 11/7/17, 3:06 AM