Odoo web 上传文件多个文件。

 

    
 @http.route()
    def portal_chatter_post(self, res_model, res_id, message, **kw):
        # result = super(MessageFilePortalChatter, self).portal_chatter_post(res_model, res_id, message, **kw);
        # return result
        if kw.get('ufile'):
            attachment_ids = []
            files = request.httprequest.files.getlist('ufile')
            Model = request.env['ir.attachment']
            for ufile in files:
                filename = ufile.filename
                if request.httprequest.user_agent.browser == 'safari':
                    # Safari sends NFD UTF-8 (where é is composed by 'e' and [accent])
                    # we need to send it the same stuff, otherwise it'll fail
                    filename = unicodedata.normalize('NFD', ufile.filename)
                try:
                    attachment = Model.create({
                        'name': filename,
                        'datas': base64.b64encode(ufile.read()),
                        'datas_fname': filename,
                        'res_model': res_model,
                        'res_id': int(0)
                    })
                except Exception:
                    _logger.exception("Fail to upload attachment %s" % ufile.filename)
                attachment_ids.append(attachment.id)
            kw['attachment_ids'] = attachment_ids;
        url = request.httprequest.referrer
        if message:
            # message is received in plaintext and saved in html
            message = plaintext2html(message)
            _message_post_helper(res_model, int(res_id), message, **kw)
            url = url + "#discussion"
        return request.redirect(url)

 

你可能感兴趣的:(odoo)