有没有办法在jQuery中上传ajax文件后刷新图像?

Cha*_*eus 5 ajax jquery image

我在页面上有一个图像.我正在使用AJAX表单插件上传单个图片,在照片上传后我想刷新页面上已有的图像.

$("#profilePicForm").ajaxForm(function() { 
    alert("Photo updated");
    $("#newPhotoForm").slideToggle();
    $("#profilePic img").load(function() {
        $(this).hide();
        $(this).fadeIn('slow');
    }).attr('src', '/Content/UsrImg/Profiles/PhotoName.jpg');
}); 
Run Code Online (Sandbox Code Playgroud)

新上传的文件与旧文件具有相同的名称,所需的只是刷新图像的方法.由于它是相同的名称,缓存正在阻碍 - 本文中描述方法不起作用.

有任何想法吗?

Nic*_*ver 13

您链接的文章在这里不起作用,但您可以使用图像上的查询字符串来中断缓存,如下所示:

$("#profilePicForm").ajaxForm(function() { 
  alert("Photo updated");
  $("#newPhotoForm").slideToggle();
  $("#profilePic img").load(function() {
    $(this).hide();
    $(this).fadeIn('slow');
  }).attr('src', '/Content/UsrImg/Profiles/PhotoName.jpg?' + new Date().getTime());
}); 
Run Code Online (Sandbox Code Playgroud)

这会强制浏览器再次检查图像,因为时间戳正在添加到查询字符串中,每次都不同.这意味着浏览器再也不能信任缓存了,因为这是一个稍微不同的服务器请求......所以它会导致你想要的重新获取.