'JavaScript Express MySQL retrieves BLOB data as Buffer type. How to get as BLOB type?
I want to retrieve the BLOB from database as BLOB type but not Buffer type because the BLOB has a file type. The problem is I can update the BLOB file to database but when I retrieve the BLOB file, it represented as Buffer type instead. How can I get as BLOB file directly because I don't want to define type as only .JPG. I want to support the .PNG, .GIF .WEBP and vice versa.
The query result is Buffer type, not Blob type from console.log()
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e2 02 28 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 02 18 00 00 00 00 04 30 00 00 ... 1152932 more bytes>
Here is my code for converting Buffer file to Base64 format then send it to the front-end for display as profile and profile setting for verifying an image upload.
getAccountByUserID = (id,success) => {
this.db.query(`SELECT u.UserID,u.Firstname,u.Lastname,u.Email,r.Role FROM user AS u JOIN role AS r ON u.UserID = r.UserID WHERE u.UserID = ?`,[id],(err,result)=>{
if (err) throw err
const account = result[0]
this.db.query(`SELECT Image,Format,Data FROM profile WHERE UserID = ?`,[id],(err,result)=>{
if (err) throw err
const buffer = result[0]["Image"]
const format = result[0]["Format"]
const image = `data:image/jpg;base64,${buffer.toString("base64")}`
const data = JSON.parse(result[0]["Data"])
success({...account,["Image"]:image,["Data"]:data})
})
})
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


