I already saw a topic around here talking about the same problem, but the resolution doesn't apply to me cause because I am trying to open an image that has a variable number of bytes.
The code:
router.get('/foto', function (req, res) {
const fdb = req.session.subdomain
const product = req.query.product
firebird.attach(conn(fdb), function (err, conn) {
if (err)
throw err;
conn.query('SELECT photo FROM product WHERE product = ?', product,
function (err, result) {
if (err) {
res.status(200).send(err);
} else {
console.log(result[0].PHOTO)
res.json(result[0].PHOTO)
conn.detach();
}
})
})
})
It returns null if the product doesn't have a photo, and if it has returns:
[Function (anonymous)]
I need to know how to read it as blob to convert to base64, cause if I try to do this with "[Function (anonymous)]", the conversion of blob to base64 gives me "Error: Invalid BLOB handle".
The function to convert blob:
function readBlob(blob) {
if (blob == null) {
return null
}
blob(function (err, name, e) {
let buffers = [];
e.on('data', function (chunk) {
buffers.push(chunk);
});
e.on('end', function () {
let buffer = Buffer.concat(buffers);
console.log(buffer)
return buffer.toString('base64')
});
});
}
I'm using Node.js, Express, and the npm module node-firebird.
I found the answer in this topic of C#. The problem is in my query, in Firebird I have to specify the format of data I want it to return, so I changed my select to this:
SELECT cast(photo as BLOB SUB_TYPE BINARY) photo FROM product WHERE product = ?
and then when I used my "readBlob" function it worked.