Uploading base64 images to Azure Storage using Next.js is a breeze!

After attempting various alternatives to solve this issue, I discovered that many of them are outdated. Even though the code below runs without any errors and successfully uploads the image while setting the mimetype correctly, it fails to render as an image. The base64 content is present within the file but does not display in any image editor or browser.

This is a snippet of what the image data looks like upon uploading:

{
  type: 'Buffer',
  data: [
    137,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,
     73,  72,  68,  82,   0,   0,   4,  56,   0,   0,   3,  96,
      8,   6,   0,   0,   0, 201, 106, 216, 155,   0,   0,   0,
      9, 112,  72,  89, 115,   0,   0,  11,  19,   0,   0,  11,
     19,   1,   0, 154, 156,  24,   0,  15, 227,   3,  73,  68,
     65,  84, 120, 156, 204, 253, 119, 184, 157,  85, 181, 254,
     15, 223, 171, 239, 222,  83,   8, 189,  10, 130, 224,   1,
     81, 144,  42, 136, 132, 162,  71,  81, 241, 128, 128, 160,
    210, 148,  46,  82,
    ... 1041133 more items
  ]
}

Below is the code snippet I am using:

const data = req.files?.file.data.toString('base64');
const uploadBlobResponse = await blockBlobClient.upload(data, data.length, { blobHTTPHeaders: { blobContentType: body.mimeType }});

I also attempted to use uploadData, but encountered the following error:

Error uploading file TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined

It's quite amusing because when I check the type of data using typeof data, it indeed returns a type of string.

Your assistance would be greatly appreciated as I have been struggling with this for hours on end.

Answer №1

Azure Blob Storage functions differently than you might expect. The blob content actually represents the data that has been uploaded. In this scenario, the binary data is being converted into a base64 string and then uploaded as a string to the blob storage.

There are a few approaches you can take:

  • Store binary data. Instead of converting the data to a base64 encoded string, you have the option to store it as a byte array. This way, you can easily display the image by using something like
    <img src="your-blob-url" />
    .
  • Read data in your application. If you prefer to store the data as a base64 encoded string, you will need to read the contents of the blob and use a method similar to
    <img src="data:blob-content-type;base64,your-base64-encoded-content" />
    in order to display it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the issue preventing me from updating an npm library to the latest version and keeping me stuck with an older version?

Currently, I am using Node v14.16.1 and npm 7.9.0 I'm trying to update the Firebase-tools library by running either sudo npm upgrade firebase-tools -g or sudo npm install -g firebase-tools to ensure that I have the latest version of firebase-tools. ...

What is the correct way to utilize window.scrollY effectively in my code?

Is it possible to have a fixed header that only appears when the user scrolls up, instead of being fixed at the top by default? I've tried using the "fixed" property but it ends up blocking the white stick at the top. Adjusting the z-index doesn&apos ...

Encountering a "code redeemed error" when utilizing Google OAuth

Currently, I am working on a project that involves user login through a Google account on localhost. The Google signup functionality has been successfully implemented. However, upon logging in from my account, I encountered the following error message: To ...

I encountered an error when attempting to use Router from next/router

In my latest project using Nextjs and Zustand, I've encountered a problem with next/router. With my current Zustand setup, I'm unable to destructure router from useRouter, so instead, I've resorted to directly importing the Router object in ...

Ways to stop Next JS 13 from automatically preloading pages

While using my app that is connected to Firebase, I have noticed that each time a page loads, my usage count exceeds due to the Next JS preloading feature. This unexpected increase in cost is concerning. How can I prevent Next JS 13 from preloading? I susp ...

Troubleshooting CORS errors in axios requests within a Next.js application

Encountering an issue while attempting to make an axios call to my API on a different localhost. How can this be resolved? The tech stack being used includes Next.js, TypeScript, and Axios. Below is the function which - although written poorly for testing ...

Declining the request to incorporate an external website into my web platform

While checking the console errors in Google Chrome, I encountered the following error message: The page 'https://website.com' was blocked from framing because a higher-level ancestor violates the Content Security Policy directive: "frame-an ...

Encountering error message "no such file or directory" while attempting to run lerna bootstrap

Struggling to execute the following command lerna bootstrap --hoist On a project I downloaded from GitHub. The steps are to Download Then run lerna bootstrap --hoist However, every time I try running the lerna bootstrap --hoist command, it fails with an ...

What is the best way to manage the parameter in a route?

I set up a few routes : var express = require('express'); var router = express.Router(); router.post('/isAccessible/:orgaId', function(req, res, next) { res.send("------ param = "+orgaId); }); module.exports = router; Next, in a ...

obtain the node variable within a React component

Starting an electron app using https://github.com/electron-react-boilerplate/electron-react-boilerplate Utilizing child_process from node like : function func() { spawn('powershell.exe', ["ls"], (error, stdout, stderr) => { if (error) { ...

What could be causing my 3D model to not align with what it's supposed to look like

I have been working on a website using React and Next.js, and I am trying to incorporate a 3D model into my site. After downloading a file in glb format, I noticed some discrepancies between the model I saw on this website and the one displayed on my own w ...

Having trouble with res.render() when making an axios request?

I am encountering an issue with my axios requests. I have two requests set up: one to retrieve data from the API and another to send this data to a view route. const response = await axios({ method: 'POST', url: 'http:// ...

There seems to be an issue with the location.href function in the server-side code of

I'm encountering an issue in the code below where I am attempting to redirect to the login page, but it seems to not be functioning as expected. app.get('/current-pass', (req, res) => { res.sendFile(path.join(staticPath, "currentPass ...

Generating a File that Imports Components and Instantly Exports Them Once More

Having trouble with this code. Initially, I had: // file 1 import Box from '@/components/Box' import Popup from '@/components/Popup' const MDXComponents = { Box, Popup } // using MDXComponents somewhere in file 1 Now, to manage t ...

Is there a solution for the error "Unable to persist the session" in a Next.js application that utilizes Supabase, Zustand, and Clerk.dev for authentication?

I have successfully set up a Next.js application with Clerk.dev for authentication and Supabase for data storage. I'm also leveraging Zustand for state management. However, an error is plaguing me, stating that there's "No storage option exists t ...

Encountering issues with Prisma Client initialization during deployment of Next.js project on Caprover platform

After attempting to deploy my Next.js version 12 projects onto my Caprover server, I encountered an unexpected error related to Prisma: Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again. Is there anyo ...

Leveraging JSON Web Tokens for secure communication between Next.js frontend and backend API

Currently, I have a nodejs express back-end api that utilizes JWT + cookie for authentication. My goal is to integrate this with a nextjs front-end located on a different domain. To achieve this, I am aware that using a proxy is the solution. However, my c ...

I am encountering an error when trying to read the property 'getState' of undefined in Next.js with Redux and PersistGate

An error occurred while migrating from Create React App to Next. I am unable to use Redux and have tried multiple solutions without success. https://i.stack.imgur.com/gfOaE.png _app.js import "../styles/globals.css"; import App from "next/app"; import { ...

Having trouble resolving the module path 'fs/promises' while using Next.js and eslint

Recently, I began working on a Next.js application and successfully configured eslint to ensure code quality. Here is the list of dev dependencies for my Next.js app: "devDependencies": { "babel-eslint": "^10.1.0", &qu ...

Utilize Express and Passport to enable simultaneous login for various 'local' accounts

Apologies in advance for any language or technical errors, as English and Node are not my strong suits. I have resorted to using Google Translation for assistance since solutions within my local sphere are unavailable. EQUIPMENT USED Ubuntu 16.04 locally ...