user592704
user592704

Reputation: 3704

Setting <img> src to Base64-encoded image with Javascript is failing

I'm trying to set <img> src with JavaScript, but the image is missing visually; it is empty. The image URL after running this code starts with "http://127.0.0.1:8080/%27" (I don't even know where this came from) instead of "data: image/png". The variable mySrc is set to a Base64-encoded image.

document.getElementById(id).src="'"+mySrc+"'";

This is what the URL looks like after setting it:

http://127.0.0.1:8080/%27data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4RMNRXhpZgAASUkqAAgAAAAQAAABAwABAAAAuAsAAAEBAwABAAAA9gkAAAIBAwADAAAAzgAAAAMBAwABAAAAAQAAAAYBAwABAAAAAgAAAA4BAgAfAAAA1AAAABIBAwABAAAAAAAAABUBAwABAAAAAwAAABoBBQABAAAA9AAAABsBBQABAAAA/AAAABwBAwABAAAAAQAAACgBAwABAAAAAgAAADEBAgALAAAABAEAADIBAgAUAAAAEAEAADsBAgAEAAAAUm9uAGmHBAABAAAAJAEAAHwBAAAIAAgACABJbWFnZSBjb252ZXJ0ZWQgdXNpbmcgaWZmdG9hbnkAAMDGLQAQJwAAwMYtABAnAABQaWNhc2EgMy4wAAAyMDA4OjA2OjA2IDEwOjM2OjEyAAQAAaADAAEAAAD...

Upvotes: 2

Views: 7590

Answers (1)

abuduba
abuduba

Reputation: 5042

You should set src without apostrophes:

document.getElementById(id).src=mySrc;

Upvotes: 2

Related Questions