コンテンツへスキップ
メニュー
この質問にフラグが付けられました
3 返信
9999 ビュー

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
1782
2
5月 25
9102
3
3月 24
4671
0
12月 23
1559
1
7月 23
1608