How do I convert base64 image to a temporary file?

My main question is how to convert base64 image to a temporary file with url using the File Helper piece, but I also been having an issue trying to upload the base64 image to NocoDB using the code piece. I figured I would just try to upload to NocoDB using a file url instead, which is the main topic of the post. When I paste the base64 image “data:image/png;base64,ixWfaaf…” into the File helper, it just produces an unreadable png file.

The issue I was having with the code piece is documented below.

The post I made on the Github:

I’ve been trying to find a way to upload an image to NocoDB using the code piece for the past 3 days but have failed tremendously. I’m trying to use the QRCode npm package to generate qr codes, but the package returns the image in base64. I was having issues with sending a POST request to the API so I figured it was an issue with the image being in base64.

After some research, I notice some people converting the base64 into a blob, then the blob to a buffer before sending a post request with axios. When I attempt to convert the blob to a buffer, it causes an “Invalid or unexpected token” syntax error. Commenting out the line where I convert the “arrayBuffer” variable to a Buffer removes the error.

CODE:

import QRCode from 'qrcode';
import { Base64 } from 'js-base64';
import axios from 'axios';
import FormData from 'form-data';
import Buffer from 'buffer';

export const code = async (inputs) => {

    const QRCodeBase64 = await QRCode.toDataURL("test"); 
    const QRCodeBinary = await Base64.atob(QRCodeBase64);
    const QRCodeBase64_No_Prefix = QRCodeBase64.replace("data:image/png;base64,", "");
    const QRCodeBinary2 = await Base64.atob(QRCodeBase64_No_Prefix);
    const QRCode_Unit8Array = await Base64.toUint8Array(QRCodeBase64_No_Prefix);

    const blobFile = new Blob([QRCode_Unit8Array], {type: "image/png"});
    const arrayBuffer = await blobFile.arrayBuffer();

    const formData = new FormData();
    formData.append("file", Buffer.from(arrayBuffer));
    // formData.append("mimetype", "image/png");

    // const response = await axios({
    //     url: 'https://nocodb.dexo.digital/api/v2/storage/upload',
    //     data: formData, 
    //     headers:{ 
    //         'Content-Type': 'multipart/form-data',
    //         'xc-auth': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    //     },
    //     method: 'post'
    // })
    // console.log(response);
    return true;
};

ERROR:

{“stack”:“/usr/src/app/dist/packages/server/api/sandbox/0/codes/step_4/index.js:2\n throw new Error('Compilation Error:\n ^^^^^^^^^^^^^^^^^^^\n\nSyntaxError: Invalid or unexpected token\n at internalCompileFunction (node:internal/vm:73:18)\n at wrapSafe (node:internal/modules/cjs/loader:1274:20)\n at Module._compile (node:internal/modules/cjs/loader:1320:27)\n at Module._extensions…js (node:internal/modules/cjs/loader:1414:10)\n at Module.load (node:internal/modules/cjs/loader:1197:32)\n at Module._load (node:internal/modules/cjs/loader:1013:12)\n at Module.require (node:internal/modules/cjs/loader:1225:19)\n at require (node:internal/modules/helpers:177:18)\n at (webpack://activepieces/engine/src/lib/handler/code-executor.ts:41:65)”,“message”:“Invalid or unexpected token”}

Duplicate of

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.