Refresh image with a new one at the same url

2018-12-31 03:02发布

I am accessing a link on my site that will provide a new image each time it is accessed.

The issue I am running into is that if I try to load the image in the background and then update the one on the page, the image doesn't change--though it is updated when I reload the page.

var newImage = new Image();
newImage.src = "http://localhost/image.jpg";

function updateImage()
{
if(newImage.complete) {
    document.getElementById("theText").src = newImage.src;
    newImage = new Image();
    number++;
    newImage.src = "http://localhost/image/id/image.jpg?time=" + new Date();
}

    setTimeout(updateImage, 1000);
}

Headers as FireFox sees them:

HTTP/1.x 200 OK
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: image/jpeg
Expires: Fri, 30 Oct 1998 14:19:41 GMT
Server: Microsoft-HTTPAPI/1.0
Date: Thu, 02 Jul 2009 23:06:04 GMT

I need to force a refresh of just that image on the page. Any ideas?

16条回答
看淡一切
2楼-- · 2018-12-31 03:36
document.getElementById("img-id").src = document.getElementById("img-id").src

set its own src as its src.

查看更多
旧时光的记忆
3楼-- · 2018-12-31 03:39

I solved this problem by sending the data back through a servlet.

response.setContentType("image/png");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, must-revalidate");
response.setDateHeader("Expires", 0);

BufferedImage img = ImageIO.read(new File(imageFileName));

ImageIO.write(img, "png", response.getOutputStream());

Then from the page you just give it the servlet with some params to grab the correct image file.

<img src="YourServlet?imageFileName=imageNum1">
查看更多
君临天下
4楼-- · 2018-12-31 03:40

I used the below concept of first binding the image with a false(buffer) url and next binding it with the valid url.

imgcover.ImageUrl = ConfigurationManager.AppSettings["profileLargeImgPath"] + "Myapp_CoverPic_" + userid + "Buffer.jpg";

imgcover.ImageUrl = ConfigurationManager.AppSettings["profileLargeImgPath"] + "Myapp_CoverPic_" + userid + ".jpg";

This way, I am forcing the browser to refresh with valid url.

查看更多
不流泪的眼
5楼-- · 2018-12-31 03:41

Try adding a cachebreaker at the end of the url:

newImage.src = "http://localhost/image.jpg?" + new Date().getTime();

This will append the current timestamp automatically when you are creating the image, and it will make the browser look again for the image instead of retrieving the one in the cache.

查看更多
倾城一夜雪
6楼-- · 2018-12-31 03:44

I had a requirement: 1) can't add any ?var=xx to the image 2) it should work cross-domain

I really like the #4 option in this answer with one but:

  • it has problems working with crossdomain reliably (and it requires touching the server code).

My quick and dirty way is:

  1. Create hidden iframe
  2. Load the current page to it (yeah the whole page)
  3. iframe.contentWindow.location.reload(true);
  4. Re-set the image source to itself

Here it is

function RefreshCachedImage() {
    if (window.self !== window.top) return; //prevent recursion
    var $img = $("#MYIMAGE");
    var src = $img.attr("src");
    var iframe = document.createElement("iframe");
    iframe.style.display = "none";
    window.parent.document.body.appendChild(iframe);
    iframe.src = window.location.href;
    setTimeout(function () {
        iframe.contentWindow.location.reload(true);
        setTimeout(function () {
            $img.removeAttr("src").attr("src", src);
        }, 2000);
    }, 2000);
}

Yeah, I know, setTimeout... You have to change that to proper onload-events.

查看更多
浅入江南
7楼-- · 2018-12-31 03:45
<img src='someurl.com/someimage.ext' onload='imageRefresh(this, 1000);'>

Then below in some javascript

<script language='javascript'>
 function imageRefresh(img, timeout) {
    setTimeout(function() {
     var d = new Date;
     var http = img.src;
     if (http.indexOf("&d=") != -1) { http = http.split("&d=")[0]; } 

     img.src = http + '&d=' + d.getTime();
    }, timeout);
  }
</script>

And so what this does is, when the image loads, schedules it to be reloaded in 1 second. I'm using this on a page with home security cameras of varying type.

查看更多
登录 后发表回答