测试环境: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文件,你可以返回任意类型的文件,甚至是数据库中的二进制字段。

odoo按钮触发下载文件的更多相关文章

  1. JS实现点击按钮,下载文件

    PS:本文说的,并非如何用js创建流.创建文件.实现下载功能. 而是说的:你已知一个下载文件的后端接口,前端如何请求该接口,实现点击按钮.下载文件到本地.(可以是zip啦.excel啦都是一样) 有两 ...

  2. 10.用js下载文件(需要后端链接)

          用js下载文件 PS:本文说的,并非如何用js创建流.创建文件.实现下载功能. 而是说的:你已知一个下载文件的后端接口,前端如何请求该接口,实现点击按钮.下载文件到本地.(可以是zip啦. ...

  3. 浅谈odoo 后台与前端文件(附件)的存储与下载

    odoo 后台与前端文件(附件)存储与下载实现 笔记太多了很乱,想想还是写博客的好,慢慢更 当然了,前提是你已经配好了odoo开发环境 一.odoo后台界面实现附件的上传和下载 1).在应用中搜索下图 ...

  4. odoo 响应下载文件

    odoo中如何实现点击按钮下载文件报告.报表到页面左下角 models.py # -*- coding: utf-8 -*-from openerp import models, fields, ap ...

  5. 使用js实现点击按钮下载文件

    有时候我们在网页上需要增加一个下载按钮,让用户能够点击后下载页面上的资料,那么怎样才能实现功能呢?这里有两种方法: 现在需要在页面上添加一个下载按钮,点击按钮下载文件. 题外话,这个下载图标是引用的 ...

  6. 使用JS代码实现点击按钮下载文件

    有时候我们在网页上需要增加一个下载按钮,让用户能够点击后下载页面上的资料,那么怎样才能实现功能呢?这里有两种方法: 现在需要在页面上添加一个下载按钮,点击按钮下载文件. 题外话,这个下载图标是引用的 ...

  7. 由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。

    由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载(这样的话ajax可以传递不同的参数),但是ajax实现的文 ...

  8. php中点击下载按钮后待下载文件被清空

    在php中设置了文件下载,下载按钮使用表单的方式来提交 <form method="post" class="form-inline" role=&quo ...

  9. Js点击按钮下载文件到本地(兼容多浏览器)

    实现点击 用纯 js(非jquery)  下载文件到本地 自己尝试,加网上找了好久未果,如: window.open(url)   location.href=url   form表单提交   ifr ...

随机推荐

  1. 论文笔记:Deep feature learning with relative distance comparison for person re-identification

    这篇论文是要解决 person re-identification 的问题.所谓 person re-identification,指的是在不同的场景下识别同一个人(如下图所示).这里的难点是,由于不 ...

  2. @ApiModelProperty的用法

    @ApiModelProperty()用于方法,字段: 表示对model属性的说明或者数据操作更改 value–字段说明 name–重写属性名字 dataType–重写属性类型 required–是否 ...

  3. java集合(list,set,map)

    集合 集合与数组 数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数情况下使用. ...

  4. vue的配置环境篇

    1.电脑已经安装的nodejs和webpack. 2.1)打开cmd.win+r.可以直接输入node -v查看版本.安装淘宝镜像  npm install -g cnpm --registry=ht ...

  5. Linux 服务器运行健康状况监控利器 Spotlight on Unix 的安装与使用

    1.本文背景 1.1.Linux 服务器情况 # cat /etc/issueRed Hat Enterprise Linux Server release 6.1 (Santiago)Kernel ...

  6. 利用web.py快速搭建网页helloworld

    访问web.py官网 http://webpy.org/ 根据网站步骤,利用 pip install web.py 若没有 PIP 则先安装pip 运行 sudo apt-get install py ...

  7. apache 配置反向代理 设置

    1.下载 安装 下载地址:http://httpd.apache.org/download.cgi 将apache 安装到某个目录中 修改conf/http.conf文件 修改配置文件端口  (端口为 ...

  8. Web.Config加密与解密

    可以使用受保护配置来加密 Web 应用程序配置文件(如 Web.config 文件)中的敏感信息(包括用户名和密码.数据库连接字符串和加密密钥).对配置信息进行加密后,即使攻击者获取了对配置文件的访问 ...

  9. ThreadLocal, HandlerThread, IntentService

    1. ThreadLocal用法详解和原理https://www.cnblogs.com/coshaho/p/5127135.html // ThreadLocal methods: public T ...

  10. Windows上为Apache配置HTTPS

    Windows上为Apache配置HTTPS   转 https://www.cnblogs.com/tianzijiaozi/p/7582671.html   1. 安装OpenSSL: Windo ...