I'm trying to get a code to allow users to upload images in some website module, that is the solution:
And with that code you can upload a file to ir.attachment.
-------------------------------xml------------------------
<form t-attf-action="/someurl" method="post" role="form" nctype="multipart/form-data">
<span class="btn btn-primary btn-file mt16">
Upload picture
<input type="file" name="picture"/>
</span>
</form>
------------------------------------------------------------
---------------------------controller-----------------------
@http.route('/someurl', type='http', auth="user", methods=['POST'], website=True)
def image_handle(self, **post):
post_file = [] # List of file to add to ir_attachment once we have the ID
post_description = [] # Info to add after the message
values = {}
for field_name, field_value in post.items():
if hasattr(field_value, 'filename'):
post_file.append(field_value)
Forum = request.registry['forum.forum']
for field_value in post_file:
attachment_value = {
'name': field_value.filename,
'res_name': field_value.filename,
'res_model': 'forum.forum',
'res_id': forum.id,
'datas': base64.encodestring(field_value.read()),
'datas_fname': field_value.filename,
}
request.registry['ir.attachment'].create(request.cr, SUPERUSER_ID, attachment_value, context=request.context)
Many thanks for your collaboration!
I'm looking exactly for the same.
If I get it work I'll post it here, do the same if you get it :)