Hi, modifying the web base file web/static/src /js/fields/relational_fields.js
I am resizing the images on the client side using the many2many_binary widget managed to modify it but it has problems since when it selected 10 images for example it only shows me 8 images that were uploaded to the analysis in the console it shows me that 10 images were loaded but it shows less I think it is not possible to climb due to lack of time I hope you can help me thanks.
_onFileChanged: function (ev) {
var self = this;
var files = ev.target.files;
var attachment_ids = this.value.res_ids;
// Don't create an attachment if the upload window is cancelled.
if(files.length === 0)
return;
var dataurl=null;
var filesnew=[];
var filesname=[];
var finaliza=false;
var conteo=0;
_.each(files, function (file) {
var record = _.find(self.value.data, function (attachment) {
return attachment.data.name === file.name;
});
if (record) {
var metadata = self.metadata[record.id];
if (!metadata || metadata.allowUnlink) {
// there is a existing attachment with the same name so we
// replace it
attachment_ids = _.without(attachment_ids, record.res_id);
self._rpc({
model: 'ir.attachment',
method: 'unlink',
args: [record.res_id],
});
}
}
if (file.type.match('image.*')) {
var MAX_WIDTH = 800;
var MAX_HEIGHT = 600;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
var img = new Image();
img.src = e.target.result;
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
img.onload = function(){
var height = img.height;
var width = img.width;
ctx.drawImage(img, 0, 0);
if (width > MAX_WIDTH || height> MAX_HEIGHT ) {
if (width > MAX_WIDTH) {
var scaleFactor = MAX_WIDTH / width;
width = MAX_WIDTH;
height = height * scaleFactor;
} else if (height > MAX_HEIGHT) {
var scaleFactor = MAX_HEIGHT / height;
height = MAX_HEIGHT;
width = width * scaleFactor;
}
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
}
dataurl = canvas.toDataURL(file.type);
console.log("cantidad archivos");
console.log(files.length);
file.src = dataurl;
filesnew.push(dataurl);
filesname.push(file.name);
conteo++;
if(files.length == conteo){
finaliza=true;
}
};//fin onload image
};//fin onload reader
} else { //onload image type
conteo++;
}
self.uploadingFiles.push(file);
});
this._setValue({
operation: 'REPLACE_WITH',
ids: attachment_ids,
});
var thiss=this;
var interv = setInterval(function(){
console.log(finaliza);
console.log("SET INTERVAL");
if(finaliza){
console.log(files.length);
for(var i=0;i<filesnew.length;i++){
console.log(filesnew.src);
console.log("llama a upload");
uploadFile(filesnew[i],thiss.fileupload_id,thiss.model,filesname[i]);
}
finaliza=false;
console.log("FINALIZA");
thiss.$('.oe_fileupload').hide();
ev.target.value = "";
clearInterval(interv);
}
},3000);
function uploadFile (imagen,fileupload_id,model,name) {
console.log("ENTRA A UPLOAD");
var nombreIframe=fileupload_id;
fetch(imagen).then(res => res.blob()).then(blob => {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/web/binary/upload_attachment', true);
// define new form
var formData = new FormData();
formData.append('ufile', blob,name);
formData.append('csrf_token', core.csrf_token);
formData.append('callback',nombreIframe);
formData.append('model',model);
formData.append('id','0');
// action after uploading happens
xhr.onload = function(e) {
console.log("File uploading completed! ");
var regex = new RegExp("\"", "g");
var textojavascript=(xhr.responseText.toString().replace(regex,"'").replace("<script language='javascript' type='text/javascript'>",'').replace('</script>',''));
var html = '<html><head></head><body></body></html>';
var eScript = document.createElement("script");
var script = document.createTextNode(textojavascript);
eScript.appendChild(script);
var eHead = document.createElement("head");
eHead.appendChild(eScript);
var iframe=document.getElementById(nombreIframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();
//document.body.appendChild(iframe);
//document.getElementById("oe_fileupload_temp43").contentWindow.document.write(eHead);
var doc = iframe.contentWindow.document.head;
doc.append(eHead);
//document.getElementById("oe_fileupload_temp43")=eIframe;
};
// do the uploading
xhr.send(formData);
xhr.onreadystatechange = (e) => {
}
});
}