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
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&field=file&filename_field=name&id={{ f.id }}">Download</a></td>
</tr>
</t>
The controller takes four arguments:
model
- the name of the model with the Binary
fieldfield
- the name of the Binary
fieldid
- id of the record containing particular file.filename_field
- name of a Char
field containing file's name (optional).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With