poppel poppel - 1 month ago 11
Javascript Question

How can I set Image source with base64

I want to set the Image source to a base64 source but it does not work:

JSfiddle.net/NT9KB

<img id="img" src="" />


the JavaScript

document.getElementById("img").src = "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="

Answer

Try using setAttribute instead:

document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' );

Real answer: (And make sure you remove the line-breaks in the base64.)

Comments