Odoo Help

1

Website Image Upload

By
yuri
on 7/25/15, 3:50 AM 2,567 views

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.

Ermin Trevisan
on 7/25/15, 4:06 AM

If I get it work I'll post it here, do the same if you get it :)

yuri
on 7/25/15, 11:01 AM
0
yuri
On 7/27/15, 8:28 AM

I'm wondering how can I set a maxium size limit to the uploaded files on the python controller side, any suggestion?



0

Hi,

There is one module in which you can find exact what you want. Inside Odoo => addons => website_crm => controllers => main.py. In that you can find the method def contactus(self, **kwargs):.

You can see the code as you want that manage image / binary field from controller. In this code you can see that controller take the post info and insert it into binary field of attachment.  

Tips : When you want to submit any binary data from web form, you must need to select "multipart/form-data" value for enctype attribute in your form.

<form class="form-horizontal mt32" id="form_job" action="#" method="post" enctype="multipart/form-data">

I am sure it will help you.

Nice, I didn't check that code. I achieve to upload attachments with that module, just adding: Upload picture In the form. How I can handle images sending it with post method?

yuri
on 7/25/15, 1:30 PM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)

Stats

Asked: 7/25/15, 3:50 AM
Seen: 2567 times
Last updated: 4/20/16, 8:30 AM