如何在网站上获取odoo二进制字段下载链接

Mar*_*elo 4 odoo odoo-website

我正在尝试从网站上获取文件的下载和文件名.

模型

class Files(models.Model):
    _name = 'website_downloads.files'
    name = fields.Char()
    file = fields.Binary('File')
Run Code Online (Sandbox Code Playgroud)

CONTROLER

class website_downloads(http.Controller):
    @http.route('/downloads/', auth='public', website=True)
    def index(self, **kw):
        files = http.request.env['website_downloads.files']
        return http.request.render('website_downloads.index', {
            'files': files.search([]),
        })
Run Code Online (Sandbox Code Playgroud)

模板

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="index" name="Website Downloads Index">
            <t t-call="website.layout">
                <div id="wrap" style="margin-top:50px;margin-bottom:50px">
                    <div class="container text-center">
                        <table class="table table-striped">
                            <t t-foreach="files" t-as="f">
                                <tr>
                                    <td><t t-esc="f.name"/></td>
                                    **<td>Download</td>**
                                </tr>
                            </t>
                        </table>

                    </div>
                </div>
            </t>
        </template>
    </data>
</openerp>
Run Code Online (Sandbox Code Playgroud)

如何获取下载链接,以及将文件保存在db save de original filename中

Lud*_*mer 8

Odoo带有内置/web/binary/saveas控制器,可用于此目的:

<t t-foreach="files" t-as="f">
    <tr>
        <td><t t-esc="f.name"/></td>
        <td><a t-attf-href="/web/binary/saveas?model=website_downloads.files&amp;field=file&amp;filename_field=name&amp;id={{ f.id }}">Download</a></td>
    </tr>
</t>
Run Code Online (Sandbox Code Playgroud)

控制器有四个参数:

  • model- 带有Binary字段的模型的名称
  • field- Binary字段的名称
  • id - 包含特定文件的记录的id.
  • filename_field- Char包含文件名称的字段的名称(可选).