zoukankan      html  css  js  c++  java
  • odoo按钮触发下载文件

    测试环境:Odoo8.0

    1.在你的模块中创建一个方法,返回url

    举个例子,

    @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',}

    例中的url包含model及field、id、filename等信息,下一步将在controller方法中抓取到url

    2.创建controller类,捕获url,并下载文件

    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))])

    在上面的方法中从url中拿到ID并返回http响应。

    例子中下载的是Excel文件,你可以返回任意类型的文件,甚至是数据库中的二进制字段。

  • 相关阅读:
    程序员用HTML5给女朋友制作的3D相册
    CSS图片翻转动画技术详解
    jQuery实现的全选、反选和不选功能
    行内元素和块级元素
    &nbsp|&quot|&amp|&lt|&gt等html字符转义
    找回Git中丢失的Commit
    GIT的使用中的问题处理
    Linux下的常用指令汇总
    Pandas存储为Excel格式:单个xlsx文件下多sheet存储方法
    Python全栈开发-执行字符串形式的语句和字符串形式的表达式方法(即exec和eval方法)
  • 原文地址:https://www.cnblogs.com/zcy1103/p/10727765.html
Copyright © 2011-2022 走看看