تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
10347 أدوات العرض

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
مايو 25
2038
2
مايو 25
9443
3
مارس 24
5012
0
ديسمبر 23
1866
1
يوليو 23
1921