4

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

Mariano DAngelo
  • 920
  • 5
  • 18
  • 39

1 Answers1

9

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).
Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • Thanks! Do you know how to name the model as the original filename? – Mariano DAngelo Aug 30 '15 at 16:10
  • I don't understand the question. – Ludwik Trammer Aug 30 '15 at 16:14
  • On the model form, when the user select the file, the file is store in the database as binary string but the filename is lost, is there a way to restore that filename? – Mariano DAngelo Aug 30 '15 at 21:05
  • You need a separate `Char` field for holding the filename information. This field name should be the value of the `filename_field` parameter for the `/web/binary/saveas` url. The `Binary` field in your view takes a `filename` parameter which should be set to the same `Char` field name (for example ``). This will automatically set the filename as the field's value. If this explanation is not enough it would be helpful if you ask a separate question, as this is a separate issue. – Ludwik Trammer Aug 30 '15 at 22:20
  • @LudwikTrammer Hi Good Day!, can you help me with this I've create a download link but an Error had occured while trying to download my file "Sorry, you are not allowed to access this document." tried to sudo it but it is no use – Black and White Aug 16 '16 at 12:20
  • @BlackandWhite the object obviously needs to be publicly accessible. Check both your access rules and record rules. – Ludwik Trammer Aug 16 '16 at 13:21