我试图用从数据库中获取的base64 BLOB更新背景图像。
HTML格式:

<div valign="bottom" class="profile-photo card-header color-white no-border"></div>

查询:
var profilePhoto = data.profilePhoto;

$(".profile-photo").css("background-image","url(data:image/png;base64," + profilePhoto + ")");

profilePhoto肯定包含二进制数据,由console.log()验证,但无论出于什么原因,这都不起作用!

最佳答案

您应该从传递给:函数的第一个参数background-image中删除多余的冒号css()

$(".profile-photo").css("background-image","url(data:image/png;base64," + profilePhoto + ")");

而不是:
$(".profile-photo").css("background-image:","url(data:image/png;base64," + profilePhoto + ")");
_________________________________________^

注意:请先使用btoa()对字符串进行编码:
var profilePhoto = btoa(data.profilePhoto);

希望这有帮助。

07-24 22:03