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

Press a button and download a file, how?

By
E.M.
on 11/1/15, 2:21 PM 3,164 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

2

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5995
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

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.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 11/2/15, 12:27 AM

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
2

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

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?

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

2 follower(s)

Stats

Asked: 11/1/15, 2:21 PM
Seen: 3164 times
Last updated: 1/27/16, 4:06 PM