Created
February 5, 2018 17:29
-
-
Save eneldoserrata/c67dcafd63f3f775f4ae7db9800f84e9 to your computer and use it in GitHub Desktop.
Como decargar archivos desde odoo usando una URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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