browserfrontendbase64

How to replace in frontend Buffer.from(data, 'base64')?


To convert a binary string into an image, I use the Buffer.from(data, 'base64') construction in Node JS, it works perfectly. Now I'm trying to pass a similar string to the browser using this

<img src="data:image/jpeg;base64,${data}" />

and it doesn't work. In the API the ${data} looks like this:

"thumbnailPhoto":{"type":"Buffer","data":[255,216,255,225,12,239,69,120,105,102,0,0,73,73,42,0,8,0,0,0,16,0,0,1,

Solution

  • Since I didn’t find anything smart, I just changed it and decided to do this formatting on the backend side (Node JS)

      const getUsersAttr = new Promise((resolve, reject) => {
        const ad = new ActiveDirectory(connLDP)
        ad.findUsers(fltLDP, function (err, users) {
          if (err) {
            reject(console.error(JSON.stringify(err.message)))
          } else if (!users || users.length == 0) {
            resolve('No users found')
          } else {
            for (let i = 0; i < users.length; i++) {
              if (users[i].thumbnailPhoto) {
                users[i].thumbnailPhoto = Buffer.from(users[i].thumbnailPhoto).toString('base64')
              }
            }
            resolve(JSON.parse(JSON.stringify(users)))
          }
        })
      })
    
      return await getUsersAttr
    }