Refresh image with a new one at the same url

后端 未结 19 3322
南旧
南旧 2020-11-21 22:00

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 backgr

相关标签:
19条回答
  • 2020-11-21 22:32

    The following code is useful to refresh image when a button is clicked.

    function reloadImage(imageId) {
       imgName = 'vishnu.jpg'; //for example
       imageObject = document.getElementById(imageId);
       imageObject.src = imgName;
    }
    
    <img src='vishnu.jpg' id='myimage' />
    
    <input type='button' onclick="reloadImage('myimage')" />
    
    0 讨论(0)
  • 2020-11-21 22:34
    <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.

    0 讨论(0)
  • 2020-11-21 22:35

    What I ended up doing was having the server map any request for an image at that directory to the source that I was trying to update. I then had my timer append a number onto the end of the name so the DOM would see it as a new image and load it.

    E.g.

    http://localhost/image.jpg
    //and
    http://localhost/image01.jpg
    

    will request the same image generation code but it will look like different images to the browser.

    var newImage = new Image();
    newImage.src = "http://localhost/image.jpg";
    var count = 0;
    function updateImage()
    {
        if(newImage.complete) {
            document.getElementById("theText").src = newImage.src;
            newImage = new Image();
            newImage.src = "http://localhost/image/id/image" + count++ + ".jpg";
        }
        setTimeout(updateImage, 1000);
    }
    
    0 讨论(0)
  • 2020-11-21 22:35

    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.

    0 讨论(0)
  • 2020-11-21 22:36

    I improved the script from AlexMA for showing my webcam on a web page wich periodically uploads a new image with the same name. I had issues that sometimes the image was flickering because of a broken image or not complete (up)loaded image. To prevent flickering I check the natural height of the image because the size of my webcam image did not change. Only if the loaded image height fits the original image height the full image will be shown on page.

      <h3>Webcam</h3>
      <p align="center">
        <img id="webcam" title="Webcam" onload="updateImage();" src="https://www.your-domain.com/webcam/current.jpg" alt="webcam image" width="900" border="0" />
    
        <script type="text/javascript" language="JavaScript">
    
        // off-screen image to preload next image
        var newImage = new Image();
        newImage.src = "https://www.your-domain.com/webcam/current.jpg";
    
        // remember the image height to prevent showing broken images
        var height = newImage.naturalHeight;
    
        function updateImage()
        {
            // for sure if the first image was a broken image
            if(newImage.naturalHeight > height)
            {
              height = newImage.naturalHeight;
            }
    
            // off-screen image loaded and the image was not broken
            if(newImage.complete && newImage.naturalHeight == height) 
            {
              // show the preloaded image on page
              document.getElementById("webcam").src = newImage.src;
            }
    
            // preload next image with cachebreaker
            newImage.src = "https://www.your-domain.com/webcam/current.jpg?time=" + new Date().getTime();
    
            // refresh image (set the refresh interval to half of webcam refresh, 
            // in my case the webcam refreshes every 5 seconds)
            setTimeout(updateImage, 2500);
        }
    
        </script>
    </p>
    
    0 讨论(0)
  • 2020-11-21 22:37

    Simple solution: add this header to the response:

    Cache-control: no-store
    

    Why this works is clearly explained at this authoritative page: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

    It also explains why no-cache does not work.

    Other answers do not work because:

    Caching.delete is about a new cache that you may create for off-line work, see: https://web.dev/cache-api-quick-guide/

    Fragments using a # in the URL do not work because the # tells the browser to not send a request to the server.

    A cache-buster with a random part added to the url works, but will also fill the browser cache. In my app, I wanted to download a 5 MB picture every few seconds from a web cam. It will take just an hour or less to completely freeze your pc. I still don't know why the browser cache is not limited to a reasonable max, but this is definitely a disadvantage.

    0 讨论(0)
提交回复
热议问题