Skip to content

Instantly share code, notes, and snippets.

@eneldoserrata
Created February 5, 2018 17:29
Show Gist options
  • Save eneldoserrata/15846ecdb73fa9ba25ec1cbfc4a5fcd3 to your computer and use it in GitHub Desktop.
Save eneldoserrata/15846ecdb73fa9ba25ec1cbfc4a5fcd3 to your computer and use it in GitHub Desktop.
Como decargar archivos desde odoo usando una URL
class Binary(http.Controller):
@http.route('/web/binary/download_document', type='http', auth="public")
@serialize_exception
def download_document(self, model, field, id, filename=None, **kw):
""" Download link for files stored as binary fields.
:param str model: name of the model to fetch the binary from
:param str field: binary field
:param str id: id of the record from which to fetch the binary
:param str filename: field holding the file's name, if any
:returns: :class:`werkzeug.wrappers.Response`
"""
Model = request.registry[model]
cr, uid, context = request.cr, request.uid, request.context
fields = [field]
res = Model.read(cr, uid, [int(id)], fields, context)[0]
filecontent = base64.b64decode(res.get(field) or '')
if not filecontent:
return request.not_found()
else:
if not filename:
filename = '%s_%s' % (model.replace('.', '_'), id)
return request.make_response(filecontent, [('Content-Type', 'application/octet-stream'),
('Content-Disposition', content_disposition(filename))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment