How to get odoo binary field link

I am trying to download a file and file name from a website.

model

class Files(models.Model):
    _name = 'website_downloads.files'
    name = fields.Char()
    file = fields.Binary('File')

controller

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([]),
        })

template

<?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>

How can I get the download link and when saving the file in db keep the original file name

+4
source share
1 answer

Odoo comes with a built-in controller /web/binary/saveasthat can be used just for this purpose:

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

The controller takes four arguments:

  • model - model name with field Binary
  • field - field name Binary
  • id - identifier of the record containing the specific file.
  • filename_field- field name Charcontaining the file name (optional).
+8
source

All Articles