[英]Replace div background-image with a blob - jQuery / CSS
I am trying to update a background image with a base64 BLOB taken from a database. 我正在尝试使用从数据库获取的base64 BLOB更新背景图像。
HTML: HTML:
<div valign="bottom" class="profile-photo card-header color-white no-border"></div>
jQuery: jQuery的:
var profilePhoto = data.profilePhoto;
$(".profile-photo").css("background-image","url(data:image/png;base64," + profilePhoto + ")");
profilePhoto
definitely contains the binary data, verifed by a console.log()
, but for whatever reason this is not working! profilePhoto
肯定包含由console.log()
验证的二进制数据,但是由于任何原因,这都不起作用!
You should remove the extra colon sign :
from the first parameter background-image
passed to css()
function : 您应该删除多余的冒号的标志
:
从第一个参数background-image
传递给css()
函数:
$(".profile-photo").css("background-image","url(data:image/png;base64," + profilePhoto + ")");
Instead of : 代替 :
$(".profile-photo").css("background-image:","url(data:image/png;base64," + profilePhoto + ")");
_________________________________________^
NOTE : Try to encode your string first using btoa()
: 注意:尝试首先使用
btoa()
对字符串进行编码:
var profilePhoto = btoa(data.profilePhoto);
Hope this helps. 希望这可以帮助。
if your profilePhoto variable getting correct value. 如果您的profilePhoto变量获得正确的值。 then only one mistake in your code.
那么您的代码中只有一个错误。 1st you need to remove colon (:) after the background-image.
首先,您需要在背景图片之后删除冒号(:)。 and also removing the white space in profilePhoto variable left and right side.like below code.
并删除profilePhoto变量左侧和右侧的空白,如以下代码所示。
$(".profile-photo").css("background-image","url(data:image/png;base64,"+profilePhoto+")");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.