Search code examples
jsonimagebase64http-postnode-red

Howto save a base64-encoded image using node-red


I wrote an Android app to send some text (which should be saved in a file) and an image (which should be saved as a seperate file) with a HTTP-POST-request using the volley lib.

The image is converted to a byte array. This byte array is converted to a string using Base64.encodeToString(). This string is set as attribute in an JSON-object which is send to the server.

The interface on the server-side is a node-red-server: The data is received and the file names and the text are extracted. So far everything works fine. But when saving the image (which is a Base64-encoded string) only writes the string in a jpg-file.

What's left to do is decode the image string and save it as image. I don't know how to do this. I tried this approach, but node-red does not know Image().

Also I did not fully understand, why the best way to send an image is to parse it to a byte-array and encode this array as Base64-string. If someone could explain the idea or link me a tutorial this might help me.

Please let me know if you need my app-code/node-red-flows to help.


Solution

  • There is a base64 Node-RED node (node-red-node-base64) that will convert a base64 encoded string into a NodeJS binary buffer.

    If you run your string through this before sending it to a file node you will get a correctly formatted image.