0

Is there someone can help me or give me a tutorial on how I can display the base64 string from Node.Js to JSP/HTML img tag?

Im passing base64 string to this Node.Js server from Java application.

var net = require('net');
var HOST = '127.0.0.1';
var PORT = 6969;
var cl = require('./client.js');

net.createServer(function(sock) { 
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

sock.on('data', function(data) { 
  sock.write(data);
  var client = new cl.client(data);
}); 

sock.on('close', function(data) { 
    console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
}); 

process.on('uncaughtException', function (err) {
    console.error(err.stack);
    console.log("Node NOT Exiting...");
});
}).listen(PORT, HOST); 

console.log('Server listening on ' + HOST +':'+ PORT);

2 Answers 2

0

Does the Base64 string start with data:image/jpeg;base64(the type may be different). If so, you can set the src attribute of img tag with it.

<img src="data:image/jpeg;base64XXXXXXXXX">

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

1 Comment

Yes it is, but I dont know how to pass the string from the node server to my web app.
0

If the base64 string that you are passing already has data:image/[image type];base64 prefix then you can just set it in the image source you don't need to do anything special. Or just have a method to check it and add the prefix if you don't have it.

  function setBase64ToImage(baseString){
  // data:image/[image type];base64

  if(baseString.substring(0,4) != "data"){
    baseString = "data:image/png;base64," + baseString;
  }
  return baseString;    
}

and in your HTML img src tag

<img src=setBase64ToImage(data)>

If your question is how to pass data from node.js to the html file then you should use a template like ejs or something to make it easy. https://github.com/tj/ejs or look at this question which anwers how to pass data from node to html. Variables between Node.js Server and Client

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.