Skip to Content
Menu
This question has been flagged
3 Replies
10001 Zobrazenia

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)

Avatar
Zrušiť
Best Answer

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
# ...




Avatar
Zrušiť

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'

Best Answer

@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')

Avatar
Zrušiť
Related Posts Replies Zobrazenia Aktivita
3
máj 25
1787
2
máj 25
9110
3
mar 24
4694
0
dec 23
1564
1
júl 23
1615