콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
10012 화면

I used below codes but it's not working.. other data updated successfully.

only image not upload

(I am using a custom theme)

HTML View

<input type="file" name="image" multiple="true"  data-show-upload="true" data-show-caption="true" data-show-preview="true" accept="image/*"/>

Model View

image = fields.Binary('image')

Controller view

 @http.route('/create/application', type="http", auth="public", website=True)
 def create_student(self, **kw):   
      
        name = kw.get('image').filename
        file = kw.get('image')
        

        student_val = {
            'image':base64.b64encode(file.read()),

        }

        adms_model = request.env['op.admission'].sudo().search([('application_number', '=',application_id )])
       
       adms_model.sudo().write(student_val)

아바타
취소
베스트 답변

I noticed a couple of potential issues:

  1. Make sure the HTML input field name matches the parameter name in the controller method:

    • In your HTML view, the input field is named "image": .
    • However, in your controller method, you are trying to retrieve the file using kw.get('file'). It should be kw.get('image') to match the input field name.
  2. Use the correct method to read the uploaded file:

    • In your controller method, you are using kw.get('image').filename to get the file name, which suggests that you are using Flask-like request objects.
    • In Odoo, to read the uploaded file, you should use request.httprequest.files['image'] instead of kw.get('image'). This will give you access to the file object, which you can then read and encode.

try this way:
input type="file" name="image" class="file" multiple="true" data-show-upload="true" data-show-caption="true" lass="file" data-show-preview="true" />

import base64
@http.route('/create/application', type="http", auth="public", website=True)
def create_student(self, **kw):
file = kw.get('image')
img_attachment = file.read()
request.env['op.admission'].sudo().create({
'image'
:base64.b64encode(img_attachment),
})

# Add any further processing or redirection logic as needed
# ...




아바타
취소

I'm had the some error of the author and I try the some code that you share it
I find this error can you explain me why ?
img_attachment = file.read()
AttributeError: 'str' object has no attribute 'read'

베스트 답변

@http.route('/create/application', type="http", auth="public", website=True)
 def create_student(self, **kw):

    files = request.httprequest.files.getlist('image')

        for file in files:

            partner_id = int(kw.get('partner_id'))

            attachment = file.read()

            partner = request.env['res.partner'].sudo().browse(partner_id)

            if partner:

                partner.write({'image_1920': base64.encodestring(attachment)})

                request.env.user.image_1920 = base64.encodestring(attachment)

        return request.redirect('/home')

아바타
취소
관련 게시물 답글 화면 활동
3
5월 25
1809
2
5월 25
9128
3
3월 24
4701
0
12월 23
1579
1
7월 23
1631