Hi there,
Has anybody tried to upload a file from the Odoo website, encode it in base64 to pass it along in the JavaScript and then call a controller to create a record and save the attachment?
My HTML:
<div class="form-group">
<label class="control-label" for="attachment1">Attachment 1:</label>
<input id="file1" name="file1" class="form-control" accept="image/*,application/pdf" type="file"/>
</div>
The button to submit my form (basically trigger JavaScript):
<button id="submit" class="btn btn-primary">Submit</button>
My JavaScript function:
function submitForm(){
var company_name = $('#company-name').val();
var contact_person = $('#contact-person').val();
var attachment1 = document.querySelector('#file1').files[0];
$.post('/form/submit', {
'company_name': company_name,
'contact_person': contact_person,
// This should pass the file (in base64?) to the controller
})
}
The controller that is being triggered:
@http.route('/form/submit', type='http', auth='public', website=True)
def submit_form(self, **kwargs):
values = {}
for field_name, field_value in kwargs.items():
values[field_name] = field_value
# Let us create a new lead from here
http.request.env['crm.lead'].sudo().create({
'name': values['company_name'],
'contact_name': values['contact_person']
# I should pass the file (base64?) here so it gets attached as attachment on the record
})
Could anybody tell me in which format I should pass the value, how to get & pass it in JS and then attach it as an attachment on the just create record?
Regards,
Yenthe
Hi Yenthe,
You can pass the attachment directly to the controller right, instead of going via js in between , hope you are aware of that ? are you specifically looking to go using js in between ?
Hi Niyas,
Yep I know you can do it directly through the controller. For some other requirements it has to pass by JS plus it are dynamic forms that are across multiple pages, that is why I pass it through JS first.