使用Javascript将<img/> src设置为Base64编码的图像失败

时间:2011-12-21 19:07:58

标签: javascript base64

我正在尝试使用JavaScript设置<img> src,但视觉上缺少图像;它是空的。运行此代码后的图片网址以"http://127.0.0.1:8080/%27"(我甚至不知道它来自哪里)而不是"data: image/png"开头。变量mySrc设置为Base64编码的图像。

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

这是设置后的URL:

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

1 个答案:

答案 0 :(得分:2)

您应该设置src不带撇号:

document.getElementById(id).src=mySrc;