How to download any file on button click ?

Posted on 9 November, 2015

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 of below code is written in Odoo V8.

Step 1 : Create a method inside your regular model and return URL. 

For example, 

1
2
3
4
5
6
7
 @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 other necessary informatons,  Now in next step i will catch that URL to an another controller kind of method.

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

Step 2 :  Make a controller class and catch that url and do process for download excel file.

For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 and in that controller method i will do necessary process and return file directly.

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 post will add extra spice into your knowledge and make your Odoo technical understanding more delicious.

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions. We are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech 

Post Your Review

X

Your Review has been posted

0 Comment(s)