来自base 64数据javascript的img url

时间:2014-12-27 19:43:12

标签: javascript html

我有base64数据,我需要转换为图像src 我知道格式:

document.getElementById('img').setAttribute( 'src', 'data:image/png;base64, stringdatahere' );

但我不知道如何将我的数据导入该字符串。感谢

2 个答案:

答案 0 :(得分:3)

使用字符串连接:

document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,' + dataObject.base64 );

答案 1 :(得分:2)

如果您在变量中有数据,那么只需

document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,' + dataVariableName );