Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get odoo binary field download link in website

I'm trying to get the download and filename of a file from the website.

model

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

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

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 the db save de original filename

like image 608
Mariano DAngelo Avatar asked Jan 08 '23 16:01

Mariano DAngelo


1 Answers

Odoo comes with a built-in /web/binary/saveas controller, that can be used for exactly 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 - the name of the model with the Binary field
  • field - the name of the Binary field
  • id - id of the record containing particular file.
  • filename_field - name of a Char field containing file's name (optional).
like image 154
Ludwik Trammer Avatar answered Jan 18 '23 20:01

Ludwik Trammer