How to attach file using upload button Odoo 18

CelineGeorge1 1,012 views 12 slides Mar 06, 2025
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

In this slide, we’ll discuss on how to attach file using upload button Odoo 18. Odoo features a dedicated model, 'ir.attachments,' designed for storing attachments submitted by end users. We can see the process of utilizing the 'ir.attachments' model to enable file uploads through ...


Slide Content

How to attach file using upload button Odoo 18 Enterprise

Enterprise Introduction In this slide, we’ll discuss on how to attach file using upload button Odoo 18. Odoo features a dedicated model, 'ir.attachments,' designed for storing attachments submitted by end users. We can see the process of utilizing the 'ir.attachments' model to enable file uploads through web forms in this slide.

Enterprise We need to create a field in our model to handle attachments. So, let's create a many2many field in the res.partner model for storing attachments. file_attachment_ids = fields.Many2many( 'ir.attachment' , string= 'Attachments' ) To set up the web form, we need to create an XML file. In this file, we use the 'form' tag to define the different fields we want, including the one for attaching files. The 'form' tag comes with various attributes like 'class,' 'action,' 'method,' and 'enctype.'Now, the 'enctype' attribute is particularly important when dealing with attachments. It tells the form how to handle the characters in the file. In simple terms, it ensures that the file content is not altered or encoded in a way that could cause issues when submitting it. Think of it as a special instruction to handle files properly.

Enterprise Here's an example 'form' tag with the 'enctype' attribute: <? xml version= "1.0" encoding= "utf-8" ?> <odoo> <template id= "website_partner_form" > <t t-call= "website.layout" > <div id= "wrap" class= "oe_structure oe_empty" > <section class= "s_website_form" data-vcss= "001" data-snippet= "s_website_form" > <div class= "container" > <form action= "/form/submit" method= "post" enctype= "multipart/form-data" > <input type= "hidden" name= "csrf_token" t-att-value= "request.csrf_token()" /> <div class= "col-lg-7 col-md-8" > <label class= "col-md-3 col-sm-4 control-label" for= "att" > Attach file </label> </div>

Enterprise <div class= "col-lg-7 col-md-8" > <input type= "file" name= "att" accept= "image/*,application/pdf,video/*" /> </div> <button type= "submit" class= "btn btn-primary" > Create </button> </form> </div> </section> </div> </t> </template> </odoo>

Enterprise In the provided example, The 'action' attribute ("/form/submit") specifies where the form data will be sent, while the 'method' attribute ("post") ensures that this data is not visible in the URL for security reasons. The 'enctype' attribute ("multipart/form-data") is particularly crucial for handling attachments, ensuring that the file content remains intact during the submission process. Notably, the 'csrf_token' serves as a security measure to guard against Cross-Site Request Forgery (CSRF) attacks. Within the form, a user-friendly section for attaching files is created using the 'label' and 'input' elements. The 'name' attribute ("att") is employed to identify the attached data, and the 'accept' attribute restricts file types to images, PDFs, and videos. Finally, the 'button' element initiates the form submission, commencing the process of creating and saving the submitted data.

Enterprise This will create a file input field like this. We can add the file by clicking on choose file button.

Enterprise For attaching files to partners, simply select the desired file and click "Create." This action will add the file as an attachment to the corresponding field in the partner record.

Enterprise In the controller, we need to create a function that will handle encoding the file and then saving it to the database. Below is a sample code: import base64 from odoo import http from odoo.http import route, request, Controller class FileUpload (http.Controller) : @http.route('/partner/form', type='http', auth='public', website=True) def file_upload (self, redirect=None, **kw) : current_partner_id = request.env.user.partner_id file_name = kw.get( 'att' ).filename file = kw.get( 'att' )

Enterprise attachment_id = request.env[ 'ir.attachment' ].create({ 'name' : file_name, 'type' : 'binary' , 'datas' : base64.b64encode(file.read()), 'res_model' : current_partner_id._name, 'res_id' : current_partner_id.id }) current_partner_id.update({ 'file_attachment_ids' : [( 4 , attachment_id.id)], }) return request.render( 'custom_,module.website_partner_form' )

Enterprise In the partner record, we can see the attachment added in the many2many field.

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com