Converting a base64 string to a JPG

This took me a short while to figure out. I’ve been vibe coding with the new Google AI Studio.

When a user uploads an image and that image is posted to N8N, it is in the format:-

“data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEC…..”

If you then want to get the actual file, you would use the node “Convert to File” - “Move Base64 String to File”

However, this doesn’t work correctly when your string is in the format provided by Google AI Studio.

To make it work, you have to strip the initial part of the string, so instead of:-

“data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEC…..”

You would actually use:-

“/9j/4AAQSkZJRgABAQEC…..”

I did this with a JavaScript code node:-

var myData = $(“Split Out”).item.json.files.slice(23);

return({“myData”:myData})

Basically, just removing the first few characters of the string.

I hope this can save someone some time.

Nice!

You figured out how to solve this on your use case.

1 Like