0

I am using the blob data type to store the blog data in MySql in node.js. It stores the data when I get the data from the database it returns the blob data. now I want to convert the blob data to actual string or text. I am unable to solve this small problem. KIndly tell me the easiest way to do this task.

2
  • Why blob? Consider using text instead? Commented Jun 19, 2022 at 8:18
  • actually, I am new to storing blogs in the database I am taking images and videos in the blog. that's why I am using blob. Is there any other data type to store blogs in the database? Commented Jun 19, 2022 at 8:29

2 Answers 2

1

Supposing you are receiving a Buffer from Database, you just need to use the global Buffer module to convert it to a string:

From database (store it in any type variable):

<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 58 00 00 00 e8 08 06 00 00 00 33 33 9a 80 00 00 00 09 70 48 59 73 00 00 16 25 00 00 16 25 01 ... 9679 more bytes>

Convert it to base64 (or whatever format you need):

Buffer.from(storedBlob).toString('base64')

Sign up to request clarification or add additional context in comments.

1 Comment

Complementing Buffer.from(await blob.arrayBuffer()).toString().
0

import axios, { AxiosRequestConfig } from 'axios';
import { Response } from 'express';

// ...

// Your Node.js API endpoint
app.post('/your-endpoint', async (req, res) => {
  try {
    // Make the axios call to the REST API endpoint
    const url = 'your-rest-api-endpoint';
    const graphReqObj = {}; // Your request payload

    const config: AxiosRequestConfig = {
      responseType: 'arraybuffer',
    };

    const response = await axios.post(url, graphReqObj, config);

    // Convert the response data to base64 before sending it back
    const base64Data = response.data.toString('base64');

    res.send(base64Data);
  } catch (error) {
    // Handle any errors that occurred during the request
    console.error('Error:', error);
    res.status(500).send('Internal Server Error');
  }
});

In this code, the Axios call is made with the responseType set to 'arraybuffer' to ensure that the response data is received as an ArrayBuffer. Then, the response data is converted to base64 using data.toString('base64') before sending it back in the response.

Make sure to replace 'your-endpoint' with the actual endpoint path for your Node.js API, and 'your-rest-api-endpoint' with the URL of the REST API endpoint that retrieves the image data from the database.

Incase responseType isnt passed, we get blob type request which then gets converted to string by nodejs and hence added special characters and hence it cannot be decoded on UI layer. Something like this: " PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0001 \u0000\u0000\u0001 \b\u0002\u0000\u0000\u0000 ) \u0000\u0000 \u0000IDATx w@\u0014g 8 g 7v\u0017X\u0016XzG X !\u0002\u001a[ \u001aM/ x) \u0019\u00135 _ K \\r 4 \u0018 g46\u0014\u0001AEP \"R -\u001d\u0016 ٝ \u001f \u0011 % \"< v <; \u0012\u0014E\u0001B\b \u0000\u0010B\bu\u000e\u00134B\b S \u0011B \u0004 \u0010B c\u0012 R $\b dg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.