Working with Base64 on Latenode

Hello! From time to time, users may need to work with Base64 files, whether it’s converting binary files to Base64 or, conversely, obtaining a binary file from a Base64 string—for example, a screenshot taken with Headless Browser.

Let me share some of my experiences that might be helpful.

How are files represented on Latenode?

Binary files usually have three attributes, whereas Base64 files are simply represented as strings.


Converting a Binary File to Base64

Let’s start with converting a binary file to Base64. It’s straightforward—we can easily convert your binary file into a Base64 string. To do this, you just need to ask our AI assistant (I’ve already done it for you and tested it).

Simply copy this code and replace variables path:

import fs from 'fs';

export default async function run({ execution_id, input, data, store }) {
    // Ensure the file path is passed correctly
    const path = data["{{1.result.file.content}}"];
    const fileExtension = data["{{1.result.file.extension}}"];

    if (!path) {
        throw new Error("Path to the file is missing.");
    }

    // Convert the file to base64 and return the data
    return {
        base64: fs.readFileSync(path, { encoding: 'base64' }),
        extension: fileExtension
    };
}

For this example, I generated an image in Stabiliti AI, then I substituted variable names in the code, and voilà—the result is a Base64 string!


You can verify it in a Base64 image viewer, and it works perfectly.


Converting from Base64 to a Binary File

Currently, our JavaScript can only convert a binary file into a string, but it doesn’t support creating a binary file directly from Base64. To work around this, you can upload files to a file-sharing service of your choice and get a download link, allowing you to retrieve the binary file for further processing.

Most services have certain restrictions on formats, but it’s always possible to find the right tool for your needs. In the near future, Latenode will introduce its own file conversion tools, which will make this even easier.

Here are a few services worth considering:

  • CloudConvert – a versatile file conversion service supporting various formats and base64 file uploads. It provides either a download link or the file itself in response.

  • Amazon S3 (AWS) – allows you to upload files in base64 format through REST API, then provides either a direct download link or access to the file.

  • File.io – a temporary file storage service. You can upload a file in base64 and receive a download link. Note that the file is deleted after a single download.

  • Transloadit – a cloud-based file processing service that supports base64 uploads and returns a URL for downloading the processed file.

  • PDF.co – enables conversion, compression, and manipulation of PDF files and images. It accepts files in base64 format and provides the output as either a file or a link.

  • ConvertAPI – a service for converting and processing various file formats. It supports base64 file uploads and provides a download link.

  • ImageKit.io – an image processing service that accepts base64 images and returns a URL for accessing the image.

  • Nanonets – a platform for image processing and text recognition. It supports base64 image uploads and returns processed data.

  • Api2Pdf – an API for handling PDFs, allowing you to upload a file in base64 and receive either a binary file or a link.

  • Filestack – a versatile file handling service that supports base64 uploads and provides a link to the file.


Useful Tips

In some cases, if you’re working with text files, like .txt files, you can directly retrieve the text content, which can greatly simplify your workflow. To do this, use the following code:

import fs from 'fs';

export default async function run({ execution_id, input, data, store, db }) {
    const filePath = data["{{10.result.file.content}}"];
    return {
        fileContent: fs.readFileSync(filePath, { encoding: 'utf-8' })
    };
}

If you want to dive into more complex, sequential conversions, you can also extract data from PDFs and other document formats without any issues—I’ve written an example on this here.

I hope this information is helpful. Good luck with your automations!

1 Like