İçereği Atla
Menü
Bu soru işaretlendi
3 Cevaplar
21094 Görünümler

Hi there,

Has anybody tried to upload multiple file from the Odoo website, and then call a controller with method post to create a record and save the attachment?

 The input type file in my template view

<div class="form-group row form-field o_website_form_required_custom">
                                <div class="col-md-4 col-sm-3 text-right">
                                    <label class="control-label" for="project_managment"></label>
                                </div>
                                <div class="col-md-6 col-sm-7">
                                    <input type="file" name="attachment" class="file" multiple="true" data-show-upload="true" data-show-caption="true" lass="file" data-show-preview="true"/>
                                </div>
                            </div>

in my controller

@http.route(['/upload/file/<model("model.custom"):object>/<model("model.custom2"):object2>/confirm'],type='http',auth='user',website=True)
    def convocatoria_proyecto_info(self, convocatoria, proyecto, **kwargs):
        values = {}
        if kwargs.get('attachment',False):
            Attachments = request.env['ir.attachment']
            name = kwargs.get('attachment',False).filename     
            file = kwargs.get('attachment',False)          
            project_id = proyecto.id
            attachment = file.read()
            attachment_id = Attachments.sudo().create({
                'name':name,
                'datas_fname': name,
                'res_name': name,
                'type': 'binary',  
                'res_model': 'model.model',
                'res_id': project_id,
                'datas': base64.b64encode(attachment),
            })
            values = {'attachment' : attachment_id}
            return request.render("modulename.template_to_render", value)

Could anybody tell me in which format I should pass the multiple value, how to get & pass attach it as an attachment on the just create record?

Regards

Avatar
Vazgeç

@Sehrish at least read the title! It says MULTIPLE files!!!

I'm having the same problem with multiple files. The problem is that you have only one file in your post variable. If you could get the files in a list you just call the create method in "for attachment in post.get(attachment)"

En İyi Yanıt

For uploading multiple files you need to have set the proper enctype in your form tag and have the option for multiple files in your input tag.

<form method="post" class="s_website_form" action="/new-ticket" enctype="multipart/form-data">
    <p><strong>Attachment </strong><input type="file" name="task_attachment" multiple="true"/></p>
</form>

In your controller you need to use request.httprequest.files.getlist() to get the files and just store them with a for loop.

if request.httprequest.method == 'POST':
        # ... # code that creates and fills a dictonary with validated data # ... new_task = request.env['project.task'].sudo().create(create_dict) if 'task_attachment' in request.params: attached_files = request.httprequest.files.getlist('task_attachment') for attachment in attached_files: attached_file = attachment.read() request.env['ir.attachment'].sudo().create({ 'name': attachment.filename, 'res_model': 'project.task', 'res_id': new_task.id, 'type': 'binary', 'datas_fname': attachment.filename, 'datas': attached_file.encode('base64'), })  

It's sad that something so basic for websites isn't better documented. I needed to analyze the whole http.py and go trough the wergzeug documentation to find the methods to get it working. 

Avatar
Vazgeç

It works fine for me. Thank you very much!

Nice... no problem.

İlgili Gönderiler Cevaplar Görünümler Aktivite
2
Şub 24
24613
2
Ara 21
24205
0
Oca 21
2089
1
Ağu 20
8692
2
Tem 20
4109