Overview
This lesson is part of the n8n AI Automation - Zero to Hero course, Section: 3. Deep Dive Concepts.
Watch the video above for the full tutorial, or read the written guide below.
How Does n8n Handle Files and Binary Data?
n8n stores files as binary data in a separate binary property on each item, kept apart from the item's JSON. This lets workflows download, read, transform, and upload files like CSVs, PDFs, images, and spreadsheets while still passing structured JSON alongside them.
How to Work With Files in n8n
Use Read/Write Files from Disk to load or save local files, and the HTTP Request node to download or upload them over the web. Convert between formats with Extract From File, which reads a CSV, PDF, or spreadsheet into JSON, and Convert To File, which builds a file from JSON to send onward. Always reference a file by its binary property name so the next node can find it.
How to create a file in the Code node with prepareBinaryData
Sometimes no node builds the file you need, for example a CSV with your own column headers. Well, the Code node can make it with this.helpers.prepareBinaryData(buffer, fileName, mimeType). You pass the content as a Buffer, and n8n returns the binary object that goes under the item's binary key.
const csv = 'name,email\nAsha,[email protected]';
const file = await this.helpers.prepareBinaryData(Buffer.from(csv), 'contacts.csv', 'text/csv');
return [{ json: { fileName: 'contacts.csv' }, binary: { data: file } }];
Use await, because the helper returns a promise. Name the field data, the default binary field name in nodes like Read/Write Files from Disk and the Google Drive upload, so the next node finds the file without extra setup. To read a file already in the workflow, use its partner this.helpers.getBinaryDataBuffer(itemIndex, 'data').
Both helpers are for JavaScript. n8n's docs say getBinaryDataBuffer is not supported in Python, and native Python in the Code node only offers _items and _item. The docs have no page for prepareBinaryData yet, so its signature here comes from the list of helpers n8n exposes to the Code node in its source code.
Sources: Get the binary data buffer, Understand n8n's data structure, Using the Code node and the Code node helper list in n8n's source.
Key Takeaways
- Binary data in n8n is nothing but a file: a PDF, a PNG or JPG, a video, a ZIP or a CSV. It shows in its own Binary tab, next to the Schema, Table and JSON tabs, where you can view images and download any file.
- An HTTP Request node with GET and a public file URL downloads the file. It lands in a binary field called
data, with the file name, extension, MIME type and size attached. - The Gmail node returns attachments only when you turn off Simplify and add the Download Attachments option. One email with 9 attachments is still one item, because the files belong to that email.
- A Split Out node with
$binaryas the field to split turns those attachments into separate items, one file each, ready for a loop, a Switch on file type or an upload. - The Compression node zips or gzips the files you name in a comma separated list of binary fields. Decompress gives them back as separate binary fields with a prefix, like file_0 and file_1.
- Nodes run once per input item. A Google Drive upload pointed at file_0 worked for the first item and failed on the next, so either set Execute Once in the node settings or build the field name from each item.
- Read/Write Files from Disk only works where n8n can reach the disk, so on n8n Cloud use HTTP Request, Gmail, FTP or a cloud storage node instead.
Related Lessons
- Lesson 11: How Branching works in n8n Workflows | Smart Automations with Multiple Paths
- Lesson 12: How to Use Merge Node in n8n | Combine Data Like a Pro
- Lesson 13: How to Use Set Node in n8n | Edit Fields Node | Add, Edit, Clean Data
- Lesson 14: How to Use Aggregate Node in n8n | Combine & Summarize Data
- Lesson 15: How to Use Remove Duplicates Node in n8n | Clean Your Data Fast
Next Steps
Continue your n8n journey with the full n8n AI Automation - Zero to Hero course.