Have you ever tried to send a file to an API and found that it won't accept a standard file upload? Some services require the file to be converted into a Base64 encoded string first. This can be a tricky task if you're not familiar with it. This post will guide you through the process of handling these scenarios in n8n, making file uploads to any API a breeze, regardless of the format it demands.
For the simplest cases, where you have a single file that needs to be encoded, n8n provides a dedicated solution. The "Extract From File" node is a perfect tool for this. It allows you to take a binary file and, with a simple operation setting, convert its content directly into a Base64 string that you can then use in your next node, such as an HTTP Request.
In the example we'll build, we'll start with an "HTTP Request" node to download the n8n logo from its GitHub repository. This will output the logo as binary data. We will then connect this to the "Extract From File" node, choosing the "Move File to Base64 String" operation, which will perform the Base64 encoding. To see this in action, check out the following screenshots:
You can inspect the results of this process by checking the output of each node. The "HTTP Request" node will provide the logo as binary data, while the "Extract From File" node will show the same data, but now as a Base64 encoded string, ready to be used in your workflow:
Okay, this works as expected, but what do we do if we have multiple binary items to encode? Let's check the same node we used above to handle this more complex scenario.
To simulate a situation with multiple files, we'll modify our workflow. We will change the "HTTP Request" node to download a ZIP file from the n8n demo website repository. Then, we will insert a "Compression" node to unzip the contents, which will give us a collection of binary files to work with:
Now, let's return to our "Extract from File" node and inspect its parameters. When we examine the node's settings, we can see that it only has a field for a single binary file. Let's try to run our workflow with the multiple files to see what happens. The result is likely not what we expected, as only one of the binary files is converted to Base64, while the others are left unchanged. This shows that the node isn't designed to handle multiple binary items at once:
Given this limitation, what's a good alternative? Let's search for a pure code solution. A great option is to use the Node.js built-in Buffer
class. You can read more about it in the official Buffer documentation. The Buffer
object has a toString()
method where you can specify an encoding option, and 'base64' is one of the supported formats. This seems like a perfect way to handle our multiple binary items. It's time to put this into practice and see how it works!
After a few minutes of work, the solution is ready to be implemented. Let's check out the code:
/**
* Encodes multiple binary files from an n8n input item into Base64 strings.
*
* This code assumes it is running in an n8n "Code" or "Function" node
* where 'this' refers to the node's context and 'helpers' are available.
*
* @returns {object} An object containing an array of file objects,
* each with a 'path' and 'data' (Base64 string).
*/
const results = [];
// The 'async' keyword is required for the outer function to use 'await'.
// It is assumed the surrounding function is already declared as 'async'.
for (const file in $input.first().binary) {
try {
// Retrieve the binary object for the current file key.
const bin = $input.first().binary[file];
// Use n8n's helper function to get the file buffer.
const binBuffer = await this.helpers.getBinaryDataBuffer(0, file);
// Construct the file path, handling cases with or without a directory.
const path = bin.directory
? `${bin.directory}/${bin.fileName}`
: bin.fileName;
// Push a new object to the results array.
results.push({
path: path,
data: Buffer.from(binBuffer).toString('base64'),
});
} catch (error) {
// Log any errors that occur during processing.
console.error(`Error processing file "${file}": ${error.message}`);
// You could also choose to throw the error or handle it differently here.
}
}
// Return the final object in the expected format for the next node.
return { files: results };
Now, let's try this code in a "Code" node and check the results:
We are happy to see all of our binary files correctly converted into an array of objects, with path
and data
attributes, and the data correctly encoded to Base64. This solution works perfectly and is a simple, effective way to handle multiple binary files in your workflow.