Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
10356 Tampilan

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
Buang
Jawaban Terbai

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
Buang

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'

Jawaban Terbai

@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
Buang
Post Terkait Replies Tampilan Aktivitas
3
Mei 25
2068
2
Mei 25
9448
3
Mar 24
5015
0
Des 23
1876
1
Jul 23
1935