Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4700 Lượt xem

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!
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 12 19
3050
3
thg 8 25
1588
1
thg 2 23
4453
3
thg 11 22
4134
1
thg 9 17
16687