How can I set Image source with base64

后端 未结 4 1877
抹茶落季
抹茶落季 2020-11-27 13:57

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

JSfiddle.net/NT9KB


the Ja

相关标签:
4条回答
  • 2020-11-27 14:36

    In case you prefer to use jQuery to set the image from Base64:

    $("#img").attr('src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==');
    
    0 讨论(0)
  • 2020-11-27 14:46

    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.)

    0 讨论(0)
  • 2020-11-27 14:48

    Your problem are the cr (carriage return)

    http://jsfiddle.net/NT9KB/210/

    you can use:

    document.getElementById("img").src = "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
    
    0 讨论(0)
  • 2020-11-27 14:49
    img = new Image();
    img.src = "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
    img.outerHTML;
    "<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==">"
    
    0 讨论(0)
提交回复
热议问题