Question for prepareBinaryData for custom node download file operation

Describe the problem/error/question

Hi, I am working on a custom app and I have a problem with download a file operation. It is a GET request that outputs a document, in this form (we get this if we print responseData):

I am trying to output the response document in the binary object of newItem that is pushed inside returnData array, similar to the way HttpRequest node handles binary response.

Unfortunately this approach shows a blank document if I click on ‘Download’ and open the document. It seems that it’s not converted to Buffer properly for the prepareBinaryData method.

If I use the HttpRequest node, the file is received correctly, I can download it and view the PDF file.

How can I implement the binary file logic within my operation, so it has same file output behaviour as HttpRequest node? Thank you!

Please share your workflow

if (operation === 'downloadDocument') {
					const documentID = this.getNodeParameter('documentID', i) as string;

					const newItem: INodeExecutionData = {
						json: {},
						binary: {},
						pairedItem: {
							item: i,
						},
					};

					const options: OptionsWithUri = {
						headers: {
							Accept: 'application/pdf',
						},
						method: 'GET',
						body: {},
						uri: `https://${tenant}.${server}/v1/document/id/${documentID}/download`,
						json: false,
					};

					// Fetch the binary response data
const responseData = await this.helpers.requestWithAuthentication.call(
  this,
  'weclappApi',
  options,
);

// Prepare the binary data and add it to the newItem
newItem.binary!['data'] = await this.helpers.prepareBinaryData(
  Buffer.from(responseData),
  'test.pdf',
);

// Push the newItem to the returnData array
returnData.push(newItem);
				}

Share the output returned by the last node

File is blank if I download and open it:

Information on your n8n setup

  • n8n version: 1.0.5
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: MAC

Hi @alexnemes,

pls. see this reply:
Move Binary Data

  • Kurt

Hello @kje,

I appreciate your response. The solution you provided seems to be more of a workaround rather than a comprehensive solution for my scenario involving custom node development.

I’m particularly interested in understanding the proper way to handle binary files within the context of custom node development. My goal is to effectively map the binary file to a Binary property, ensuring that users can seamlessly view and download the file from my custom node. I’ve noticed that the HttpNode handles this format well, and I’m hoping to adopt a similar approach for my node.

If you have any insights or information regarding this matter, I would greatly appreciate it. Please feel free to reach out if you need any additional information from my end. Thank you!

Hey @alexnemes,

That looks pretty similar to what I normally do and it looks like the output has the mimetype and a filesize. If you download the file is it still 80.5kb or is it 0 bytes?

1 Like

Hey @Jon

The file has 45.2 kB if I use my module to download file. If I download it and open it, it’s blank. It still has 45 KB after download:

image

If I use the http request node, with same request and parameters, file has 47.5 kB and has content when I open it.

Hey @alexnemes,

If it has a size it has some data which is handy, Can you open it in a text editor to see what is in there?

This is what’s inside:

Hey @alexnemes,

Instead of using Buffer() what happens if you try and directly write the file out?

Hi @Jon,

Then I receive this error: ‘ERROR: body.pipe is not a function’

TypeError: body.pipe is not a function
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/BinaryDataManager/utils.ts:7:13
    at new Promise (<anonymous>)
    at binaryToBuffer (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/BinaryDataManager/utils.ts:5:2)
    at BinaryDataManager.storeBinaryData (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/BinaryDataManager/index.ts:107:39)
    at setBinaryDataBuffer (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/NodeExecuteFunctions.ts:928:41)
    at prepareBinaryData (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/NodeExecuteFunctions.ts:1058:9)
    at Object.prepareBinaryData (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/NodeExecuteFunctions.ts:2268:3)
    at Object.execute (/Users/alexnemes/Documents/Projects/N8N/Weclapp/n8n-nodes-weclapp/nodes/Weclapp/Weclapp.node.ts:577:46)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/src/Workflow.ts:1249:19)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/WorkflowExecute.ts:1024:29

Code looks like this:

const responseData = await this.helpers.requestWithAuthentication.call(
  this,
  'weclappApi',
  options,
);


// Prepare the binary data and add it to the newItem
newItem.binary!['data'] = await this.helpers.prepareBinaryData(
   responseData,
  'test',
);

// Push the newItem to the returnData array
returnData.push(newItem);

Hey @alexnemes,

Interesting, You might need to do some magic then when creating the buffer. I would need to spend a bit more time on this one and maybe even play with the API but it looks like you have the data to start with and when changing it to the buffer it gets a bit confused.

What other options have you tried when working with the buffer?

Thank for the help on this one. Mainly tried the Buffer.from() method but there might also be some libraries that could help. I noticed responseData is of type string. Will also look for other approaches

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.