I have a Firebird DB table, which contains fields with image data (as blob sybtype -5). I need to get this data and convert it to an image file. Code example below creates a file (but size is very large, and while trying to open it - it is said, that file is not right BMP file). What am I missing here?
result[0].OBJ_IMAGE1((err, name, eventEmitter) => {
if(err) throw err;
let chunks = [];
eventEmitter.on('data', chunk => {
chunks.push(chunk);
});
eventEmitter.once('end', () => {
let buffer = Buffer.concat(chunks);
fs.writeFile('test.png', buffer, err => {
if(err) throw err;
console.log('image file written');
});
});
});


I have tested your code, and it will correctly write out the blob to a file. Your problem is therefore - as also discussed in the comments - one of not knowing what the actual image file type is, so you're storing it with the wrong file extension. You need to know the file type (or encoding, or encryption) used by the application storing the image data to be able to select the right file extension, image viewer to display the data and/or additional transformations required to get a viewable file.
Looking at the example data and the File Signatures link Marcodor provided, it is possible that the stored data is type "Standard JPEG file with Exif metadata" (
FF D8 FF E1 xx xx 45 78 69 66 00), but the file is prefixed with 5 bytes of application specific data (maybe some sort of type identifier, or a file length, or something like that). Based on that, try usingSUBSTRING(OBJ_IMAGE1 FROM 6)and see if saving asjpgallows you to open the file (NOTE: This doesn't necessarily mean all files stored in these blobs are JPEGs!).As an aside, it is more efficient to store the image using:
Using
pipewill write the chunks directly to a file as they are received, instead of accumulating the entire file into memory before writing it out.