2

I Would like to get the data from a post request an pass it onto an img tag. I know i could do it with an get request but i kinda need to use a post request to send the location of the file.

$.post('/getImage',{location: 'image.jpg'} ,function(image) {
    $('body').append('<img src=\'image\'></img>');
}

So is there any way to set the data from the post request to the img tag? The post request works just so you know so i don't need any help setting that up

Thomas Fellner
  • 113
  • 2
  • 11

2 Answers2

1

Return the base64 encoding of the image and set the image's src attribute like this:

$('body').append('<img src="data:image/png;base64,' + data + '" />');
Fa773N M0nK
  • 120
  • 1
  • 2
  • 9
  • I'm not familiar with node.js but a little searching showed that you can get base64 of a file like so: `return new Buffer(fs.readFileSync(file)).toString('base64');` – Fa773N M0nK Jul 22 '15 at 11:30
1

what kind of data returns from server in your callback?

you have two ways:
1) Convert file at server to base64, and create Image tag at client and set base64 to src
2) Return at server binary data, and use at client native xhrRequest, like in this post 

https://stackoverflow.com/a/17682424/5138198

Community
  • 1
  • 1
Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37