跳至内容
菜单
此问题已终结
1 回复
4696 查看

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!
形象
丢弃
最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
1
12月 19
3042
3
8月 25
1587
1
2月 23
4453
3
11月 22
4131
1
9月 17
16677