Transform image to base64 in n8n for Odoo upload

Hey everyone, I’m stuck trying to change an image to base64 in n8n so I can put it on Odoo. I’ve been at it for hours and even asked AI for help. The best code I got so far is from n8n AI, but it’s not working right. Here’s what I’m dealing with:

// Get image data
let imageData = $input.first().binary?.data;

if (!imageData) {
  throw new Error('Image data missing');
}

// imageData is already base64 in n8n
let base64Pic = imageData;

// Get today's date
let today = new Date().toISOString().split('T')[0];

// Set up data for next step
return {
  json: {
    x_studio_pic: base64Pic,
    x_studio_date: today,
    x_studio_comments: $json.x_studio_comments || '',
  },
  binary: {
    data: imageData
  }
};

The output shows some JSON and binary stuff, but it’s not quite right. The image details are there (name, type, size), but I can’t get it to work with Odoo. Any ideas on what I’m missing or doing wrong? Thanks!

I have experienced similar issues when integrating n8n with Odoo for uploading images. One important point is ensuring that the base64 string does not include the data URI prefix, such as ‘data:image/png;base64,’. Sometimes the image data, even if already in base64, needs to be decoded and then re-encoded to verify the formatting. For example, you can use a buffer:

let buffer = Buffer.from(imageData, 'base64');
let base64Pic = buffer.toString('base64');

In some cases, Odoo expects the image data to be wrapped in an array (e.g., x_studio_pic: [base64Pic]), so it is worth checking your field configuration. I hope this approach proves useful.

I’ve dealt with this exact problem before. The key is to ensure your base64 string is properly formatted for Odoo. Here’s what worked for me:

First, strip any metadata from the base64 string. Then, add the necessary prefix that Odoo expects. Something like this:

let cleanBase64 = imageData.replace(/^data:image\/(png|jpeg|jpg);base64,/, '');
let odooFormattedImage = `data:image/png;base64,${cleanBase64}`;

Also, double-check your Odoo field settings. Some versions require the image to be sent as a list, like x_studio_pic: [odooFormattedImage].

Lastly, ensure your content type headers are set correctly when making the API call to Odoo. This often solves unexpected upload issues.

hey there, i’ve had similar troubles. have u tried using the ‘Buffer’ class? it’s pretty handy for this kinda stuff. something like:

let buff = Buffer.from(imageData, 'base64');
let base64Pic = buff.toString('base64');

also, make sure ur odoo field is set up right. sometimes it wants the image as an array. good luck!