0
votes

as detailed in the subject I need some explainations about the way to download an xls file. On Odoo8, by a wizard I create an xls file with xlwt a stored it to the file system using the function wb.save(filename). But, after a lot of googling, I can't what I need and I'm really upset... Is there someone who help me putting on the right way?

1

1 Answers

0
votes

Here is perfect example to download xls file.

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

@api.multi 
def get_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',
 }                

( 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.

    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

http://www.emiprotechnologies.com/technical_notes/odoo-technical-notes-59/post/how-to-download-any-file-on-button-click-244

Using above method you can create xls,csv,txt any file.

Thanks,