我在我的服务器上有一个图片,profile.jpg。当我上传一张新照片时,用数据替换picture.jpg而不是名称(换句话说,旧的profile.jpg被新的profile.jpg取代,但新的也被称为profile.jpg)。在我的承诺返回后,我调用forceUpdate,但除非更改图像的实际url(src),否则这不会执行任何操作。 (再次从正确的URL,“空”,正确的URL)见我的代码中,我试图来连接,以便承诺,相信反应会认识到,网址正在发生变化:Reactjs数据更改,文件名保持不变
fetch('http://localhost:3000/change_pet_pic/?petID='+this.props.pet.id+'&userID='+this.props.pet.ownerID, { method: 'POST', body: form })
.then(function(res) {
return res.json();
}).then(function(json) {
var pet = $this.props.pet;
pet.petPicture = "empty";
$this.props.pet=pet;
$this.forceUpdate();
return json.picture_url;
}).then(function(url){
var pet = $this.props.pet;
pet.petPicture = url;
$this.props.pet=pet;
$this.forceUpdate();
})
谢谢您提示!
你的ReactJS代码似乎没有错,相反它应该是一个浏览器缓存,它会一次又一次地返回旧的图像,因为文件名看起来相同。你可以做的是,只要图像发生变化,你就可以用不同的查询字符串访问图像。所以第一次,你可以用'profile.jpg?v = 1'访问它,第二次,你可以用'profile.jpg?v = 2'来访问它。 – Aruna