如何在odoo中添加下载超链接以从网站下载附件

时间:2016-06-30 10:29:57

标签: odoo-9

我想在odoo中的网站模块中添加一个超链接,我可以使用它来下载附件,其中包含已创建产品的信息。请帮助解决此问题。 谢谢

4 个答案:

答案 0 :(得分:0)

回答这个问题为时已晚。无论如何,它可能对某些人有帮助。我用Google搜索了更多,因为我没有得到任何写入解决方案。

第1步: 创建下载按钮的超链接。在该按钮中,您需要传递

  1. 型号名称
  2. 字段
  3. ID
  4. filename(可选)
  5. 例如,您的链接应该是这样的

    <a t-attf-href="/web/binary/download_document?model=product.brand.files&amp;field=file&amp;id={{ file.id }}&amp;file_name={{ file.file_name }}">
    

    第2步:

    为上述链接创建控制器。实施例

    class Binary(http.Controller):
        @http.route('/web/binary/download_document', type='http', auth="public")
        @serialize_exception
        def download_document(self,model,field,id,file_name=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.env[model].sudo().search([('id', '=', int(id))])
            filecontent = base64.b64decode(Model.file or '')
            headers = werkzeug.datastructures.Headers()
            if not filecontent:
                return request.not_found()
            else:
                if not file_name:
                    filename = '%s_%s' % (model.replace('.', '_'), id)
                    headers.add('Content-Disposition', 'attachment', filename=filename)
                    return request.make_response(filecontent,headers)
                else:
                    headers.add('Content-Disposition', 'attachment', filename=file_name)
                    return request.make_response(filecontent,headers)
    

    它会起作用。以上代码为Odoo 10.您也可以对以下版本执行相同操作。我来自this link

    干杯。

答案 1 :(得分:0)

这是一个工作示例

return {
    'type': 'ir.actions.act_url',
    'url': '/web/content/%s/%s' % (attachment_id.id, shortname),
    'target': 'self',
    'nodestroy': False,
}

简而言之,您只需拥有一个指向/web/content/ir_attachment_id/the_name_you_want_the_file_to_have的链接。

这也适用于Odoo V10。

答案 2 :(得分:0)

这是odoo 10的工作,你必须尝试

<a t-attf-href="/web/content?model=website.support.ticket.message&amp;field=attachment&amp;id={{chat.id}}&amp;filename={{chat.attachment_filename}}&amp;download=true">Download</a> 

答案 3 :(得分:0)

如果这是针对表单的,则可以完全在视图中完成。首先在xml文件中的某处添加一条记录条目:

<record id="module_name.http_link" model="ir.actions.act_url">
  <field name="name">Button Name</field>
  <field name="type">ir.actions.act_url</field>
  <field name="target">new</field>
  <field name="url">https://your-http-link</field>
</record> 

然后采用以下格式:

<button type="action" name="%(http_link)d" string="Name" class="oe_highlight"/>