Hi everyone,
I have a custom code to multi attach files on Cases module. I need to create a relationship with notes (for each for the files selected per input file multiple) once save button clicked. My code on 'create.js' file is:
({
extendsFrom: 'CreateView',
initialize: function(options) {
this._super('initialize', [options]);
controller = this;
this.on("render", this.append_attachments_button, this);
this.context.on('button:save_button:click', this.save_button_click, this);
},
// Append input type file and text-area to files info
append_attachments_button: function() {
var element = controller.model.get('tct_multi_attach_c');
var id = $(element).attr('id');
$("#" + id).empty();
var controls_append = '<input type="file" id="button_files" multiple /> <br>';
controls_append += '<textarea id="info_attachments" style="resize:none;background-color:white; color:black; height:80px;" readonly></textarea>';
$("#" + id).append(controls_append);
$("#button_files").on('change', this.button_files_change);
},
// Populate text area with file's name
button_files_change: function() {
$("#info_attachments").val(fname);
var fi = document.getElementById('button_files');
if (fi.files.length > 0) {
for (var i = 0; i <= fi.files.length - 1; i++) {
var fname = fi.files.item(i).name;
var fsize = fi.files.item(i).size;
console.log(fname + " - " + fsize + " bytes");
if ($("#info_attachments").val() != "")
$("#info_attachments").val($("#info_attachments").val() + "\n" + fname);
else
$("#info_attachments").val(fname);
}
} else {
console.log('Please select a file.');
}
},
save_button_click: function() {
console.log("Save clicked");
var fi = document.getElementById('button_files');
if (fi.files.length > 0) {
for (var i = 0; i <= fi.files.length - 1; i++) {
var fname = fi.files.item(i).name;
var fsize = fi.files.item(i).size;
console.log(fname + " - " + fsize + " bytes");
// Need to create here the relationship between the new Cases record created with Notes module and upload the current file to respect directory.
var xmlHttpRequest = new XMLHttpRequest();
var file = fi.files.item(i);
var fileName = fname;
var target = fname
var mimeType = fname.toString().split('.')[fname.toString().split('.').length - 1];
xmlHttpRequest.open('POST', target, true);
xmlHttpRequest.setRequestHeader('Content-Type', mimeType);
xmlHttpRequest.setRequestHeader('Content-Disposition', 'attachment; filename="' + fileName + '"');
xmlHttpRequest.send(file);
}
}
}
})
Someone knows how can I make it?
Thak you.