How to read Binary data

I trying to read binary data and send it to other nodes. In this example, I’ll send the data as a message to Telegram.

Basic setup:

Read Binary File > Telegram

In the telegram, I try to send a message using the data read from the file as part of it., but I can’t do that. When I select to read the raw data in the path(assuming data was saved in the data key):
Nodes > Read Binary Files > Output Data > Binary > data > [More button] Raw value, just like the picture above:

image

But when I select it, It doesn’t show the binary file, but some JS object representation:

{{$node["Read Binary File"].binary.data}}
that evaluates to
{"mimeType":"text/plain","fileName":"TODO"} instead of the contents.

image

I tried other options but I couldn’t get the data.

The example flow in JSON
{
  "name": "pp testing",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "filePath": "../TODO"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "",
        "text": "={{$node[\"Read Binary File\"].binary.data}}",
        "additionalFields": {}
      },
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        550,
        300
      ],
      "credentials": {
        "telegramApi": "Daemoniac Bot"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Read Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary File": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "8"
}

You first have to use the Move Binary Data Node to move the data from binary to json. As soon as you did that, is it available in the Expression-Editor.

You can find the documentation here:

I looked at that, but It just seems to able to handle JSON format, not text formats in general. What if the binary data isn’t JSON?

The content of the binary data is written to the JSON-Structure in a variable. Then you can pass and use this variable in your telegram node.

Well, I can’t find this here. Here is the screenshot of the possible thinks to select:

image

Here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "filePath": "/tmp/test.txt"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "setAllData": false,
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Read Binary File": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I see, using the move from binary to create a JSON that contains the data in a key.

But this seems odd to me. Maybe it has as technical reason for that, but wouldn’t be better if instead of using an extra node for that, this kind of data would be available directly?

For instance, if the binary > data could have a field with the acutal data, instead of using a extra node for that? This would be way better and would make more sense. I can even volunteer to implement this as a PR.

The data is available directly, as binary data.

Sure, it would be easier in your single specific use case but as the node says does it read binary data which would normally not make sense loading it directly into the json data. For that reason does it get loaded to the binary data.
So it makes sense for a few use-cases but not for most other ones. Lets say makes sense for text-files, does not make any sense for xls, png, pdf, jpg, mov, mp3, … does it make sense for csv, not so sure, really depends.
And even for the text-file, does it really depend what exactly you are doing with the data afterward. If you need the content directly, you want it in json, but if you want to upload it directly to Dropbox or send it as a file via HTTP, not so much.