I’m using an Airtable list node to return records and will download attachments if they are present. I have a functioning workflow which send this data to a ‘Move Binary Data’ node and converts the file into a base64 string to be used later. My problem is that if more than 1 file is present, I need to be able to iterate over the binary files and convert each one - right now I am only able to access the first file (stored in ‘attachment_0’.
I have tried to write a JS function to do this and tried many iterations, but not managed to get it to work the way I need:
As a starting point the below takes an image from unsplash and converts the binary data to base64 using a code node, I suspect it should be that bad to adapt it to Airtable.
Thank you @Jon! You got me on the right track, here is the finished code snippet which iterates over the attachments returned from the Airtable list node, converts to base64 string and assigns the string to a new data property within the attachment object.
// Loop over input items
for (const item of $input.all()) {
// Get the attachments for the current item
const attachments = item.json.fields.attachments;
// Loop over each attachment
for (let i = 0; i < attachments.length; i++) {
// Get the binary data buffer for the current attachment
let binaryDataBufferItem = await this.helpers.getBinaryDataBuffer(0, `attachments_${i}`);
// Convert the binary data buffer to a base64 string
const base64Data = Buffer.from(binaryDataBufferItem).toString('base64');
// Add the base64 string to the current attachment object
attachments[i].data = base64Data;
}
}
// Return the modified input items
return $input.all();