This question has been flagged
1 Reply
3464 Views

Hi folks!

I'm trying to upload image from qweb into database which will be shown in Sales module. Following is my code for qweb form:-

<!-- Image Upload form -->
<form id="file_upload_form" t-attf-action="/website_form_test/" method="post" enctype="multipart/form-data" class="form-horizontal mt32">
<div class="row">
<div class="col-md-6">
<div>
<strong>Payment Screenshot</strong>
<input type="file" id="photo" name="photo" class="file" multiple="true" data-show-upload="true" data-show-caption="true" lass="file" data-show-preview="true" />
<button type="submit" name="screenshot" class="btn btn-primary">Upload</button>
</div>
</div>
</div>
</form>

<!-- Image Upload form -->

Following is my api which I created in main.py(controller):-

@http.route(['/website_form_test/'], method='POST', csrf=False, type='http', auth="public", website=True)
def image_upload_test1(self, **post):
article = post['photo']
article_1 = unicodedata.normalize('NFKD', article).encode('ascii', 'ignore')
article_2 = article_1.lstrip('data:image/jpeg;base64,')

add_student = request.env['button.demo']
student_data = {
'image': '/' + article_2,
}
created_id = add_student.create(student_data)
When I submit form from qweb, it's return me error:- normalize() argument 2 must be str, not FileStorage.
Please help me to sort out this issue.
Thanks in advance!
Avatar
Discard
Best Answer

Hi 
Try This,

import base64
@http.route(['/website_form_test/'], method='POST', csrf=False, type='http auth="public", website=True)
def image_upload_test1(self, **post):
          photo = post.get(
'photo')
         
if photo:
              photo_data = photo.read()
              photo_base64 = base64.b64encode(photo_data)
              photo_base64_str = photo_base64.decode(
'utf-8')
              add_student = request.env[
'button.demo'
              student_data = {
'image': photo_base64_str}
              created_id = add_student.create(student_data)

Hope it helps

Avatar
Discard