简体   繁体   中英

Firebird blob to Base64 - Node.js

I'm migrating one website with java/jsf for Node/Angular but i have the problem for convert blob from firebird database to base64 using node-firebird.

connection.query(query,function(err,rows){

  if(err) {
    res.json({"Error" : true, "Message" : "Error executing query"});
  } else {

    var buffer;
    var bufferBase64;

    for(var i = 0; i < rows.length; i++){
      rows[i].image(function(err, name, eventEmitter) {

        eventEmitter.on('data', function(chunk) {
           buffer = new Buffer(chunk, 'binary').toString('base64');
           bufferBase64 += buffer;

        });
        eventEmitter.on('end', function() {
        console.log(bufferBase64); // print base64
        });
      });

    }

    res.json(rows);
  }
  connection.detach();
});

This code from node-firebird generate a invalid base64, but this work using Java.

byte[] encoded = Base64.getEncoder().encode(rs.getBytes("image"));
System.out.println(new String(encoded));

This is a buffers of a image : [function] that returns from the database

Thanks Mark rotteveel for solved my problem.

if have someone else with the same problem, below the solution.

         rows[i].image(function(err, name, eventEmitter) {
            let buffers = [];
            eventEmitter.on('data', function(chunk) {
               buffers.push(chunk);
            });
            eventEmitter.once('end', function() {
            let buffer = Buffer.concat(buffers);
            console.log(buffer.toString('base64')); // print base64
            });
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM