Image to base 64

hi! Is it possible to read a file and convert it to base 64?

See example below

Kind regards
Mattias

{
  "nodes": [
    {
      "parameters": {
        "command": "=echo {{$node[\"HTTP Request\"].binary}} | base64"
      },
      "name": "ToBase64",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        460,
        290
      ]
    },
    {
      "parameters": {
        "command": "=echo {{$node[\"ToBase64\"].json[\"stdout\"]}} | base64 -d"
      },
      "name": "FromBase64",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        740,
        290
      ]
    },
    {
      "parameters": {
        "url": "https://static.euronews.com/articles/stories/04/76/24/34/773x435_cmsv2_7228f4d1-47e6-571b-8642-67b55beedb75-4762434.jpg",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        200,
        290
      ]
    }
  ],
  "connections": {
    "ToBase64": {
      "main": [
        [
          {
            "node": "FromBase64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "ToBase64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@Mattias_Larsson not sure if I fully understand, perhaps the example below helps.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://static.euronews.com/articles/stories/04/76/24/34/773x435_cmsv2_7228f4d1-47e6-571b-8642-67b55beedb75-4762434.jpg",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "fileName": "aja.jpg"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        770,
        300
      ]
    },
    {
      "parameters": {
        "filePath": "aja.jpg"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        1010,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write Binary File": {
      "main": [
        [
          {
            "node": "Read Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi i want to convert an image into a base64 file format then send it as base64.

On another workflow later I want to convert it back to an image.

Does it make it a bit more clear?

Yes makes sense. That can be done with the Move Binary Data Node.

Here an example:

{
  "nodes": [
    {
      "parameters": {
        "url": "https://static.euronews.com/articles/stories/04/76/24/34/773x435_cmsv2_7228f4d1-47e6-571b-8642-67b55beedb75-4762434.jpg",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        640,
        380
      ]
    },
    {
      "parameters": {
        "setAllData": false,
        "options": {
          "encoding": "utf8",
          "keepAsBase64": true
        }
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        840,
        380
      ]
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like