Hi guys,
im trying to reimplement the old web/binary/saveas controler from v8 in v10 since i need to access attachements via download link from the website.
In v8 i was able to download attachements with an url like this:
http://localhost:8069/web/binary/saveas?filename_field=datas_fname&field=datas&model=ir.attachment&id=225
At first i just copied the old code into a new module to a v10 server and got all kinds of errors.
Im stuck at the following error:
line 94, in saveasn res = Model.read([int(id)], fields)[0]nIndexError: list index out of rangen", "exception_type": "internal_error", "message": "list index out of range", "name": "exceptions.IndexError", "arguments": ["list index out of range"]}}
can someone help me figure out what list index out of range means in this particular case?
Heres the new code:
class Binary(http.Controller):
@http.route('/web/binary/saveas', type='http', auth="public")
@serialize_exception
@api.model
def saveas(self, model, field, id=None, filename_field=None, **kw):
""" Download link for files stored as binary fields.
If the ``id`` parameter is omitted, fetches the default value for the
binary field (via ``default_get``), otherwise fetches the field for
that precise record.
: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: field holding the file's name, if any
:returns: :class:`werkzeug.wrappers.Response`
"""
Model = request.env[model]
cr, uid, context = request.cr, request.uid, request.context
fields = [field]
content_type = 'application/octet-stream'
if filename_field:
fields.append(filename_field)
if id:
fields.append('file_type')
res = Model.read([int(id)], fields)[0]
if res.get('file_type'):
content_type = res['file_type']
else:
res = Model.default_get(fields)
filecontent = base64.b64decode(res.get(field) or '')
if not filecontent:
return request.not_found()
else:
filename = '%s_%s' % (model.replace('.', '_'), id)
if filename_field:
filename = res.get(filename_field, '') or filename
return request.make_response(
filecontent, [('Content-Type', content_type),
('Content-Disposition',
content_disposition(filename))])
Hi @eugen-don,
Thank you for your report but this is not an issue nor is it a bug.
The Github issue tracker is only for bugs and/or issues in the official Odoo code.
Please ask this question on the official help forum (help.odoo.com) where plenty of people can help you!
Regards,
Yenthe
in this way it worked in version 10
http://localhost:8069/web/content/?model=ir.attachment&id="+str(doc_id.id)+"&filename_field=datas_fname&field=datas&download=true&filename="+str(doc_id.name),
wow @torreslr you actually figured it out ! Many thanks my friend...
Most helpful comment
in this way it worked in version 10
http://localhost:8069/web/content/?model=ir.attachment&id="+str(doc_id.id)+"&filename_field=datas_fname&field=datas&download=true&filename="+str(doc_id.name),