我有以下 HTML 页面。
<div class="profile-photo" id="profilephoto" style="background:url('data:image/png;base64,iVBORw0KGgoA') no-repeat 50% 50%;"></div>
使用 jQuery,我尝试替换这个 base 64 值,即 "iVBORw0KGgoA"。
因此,在成功上传个人资料图片后,我尝试用新上传的图片替换此预览。这适用于第一次上传,但从第二次开始,使用不同的图像重新上传不会替换此图像预览。
使用以下 ajax 调用,我采用新的 base 64 值并在其成功调用中替换它:
$.ajax({
type: "GET",
url: '@Url.Action("GetProfilePictureValue", "Student")',
data: { "StudentId": studentID },
dataType: "json",
success: function (newProfilePictureBase64Value)
{
$("#profilephoto").css('background', 'none');
$('#profilephoto').css('background', 'url(data:image/png;base64,' + newProfilePictureBase64Value + ') no-repeat 50% 50%;');
$("#profilephoto").load(location.href + " #profilephoto>*", "");
},
error: function ()
{
alert('Error occured');
}
});
最佳答案
您尝试清除浏览器的缓存吗?
关于javascript - div背景图片不改变重新上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541572/