1

I'm trying to retrieve an image to a html page with GET request like this:

let img = new Image();
img.onload = function () { ... };
img.src = 'http://localhost:5000/img';

and it works great on the first time when you start the server, but when I try to do this again, it returns the previous image.

My Flask part of '/img' looks like this:

@app.route('/img')
def get_img():
    return send_from_directory('./tmp', 'result.png')

I made sure that the result.png on my computer is being updated, and it is. When I try to manually access 'http://localhost:5000/img', it retrieves the old image, that I've already overwritten. When I refresh 'http://localhost:5000/img', it returns the updated image that's on my computer at the moment.

This probably has nothing to do with Flask, but instead with my lacking knowledge of how HTTP works, and I'd really appreciate if someone could help me out on this one. Thanks.

nameisxi
  • 163
  • 1
  • 1
  • 10
  • The reason is cache. The cache file of the image not updated yet. Thats why you are getting the old image. Actually the image is comming from cache (not comming from source). you can try with some get parameters like version or anything else, Like `http://localhost:5000/img?v=1`... To get updated image `http://localhost:5000/img?v=2`. you can see this types of URL in many websites, Like `./js/main.js?v=1.0` OR `./css/main.css?v=1.0`.. They use this trick to bypass cache. – BadPiggie Nov 22 '18 at 07:17
  • 1
    Another thing, You can tell your browser to `do not cache http://localhost:5000/img only`. Then your browser will not cache the image.. Refer this : [disable cache for specific page only - flask](https://stackoverflow.com/questions/28627324/disable-cache-on-a-specific-page-using-flask) – BadPiggie Nov 22 '18 at 07:18
  • Thanks! I tried to add those tags to the HTML file. So far it's not working, but I'll probably figure it out. – nameisxi Nov 22 '18 at 11:27

0 Answers0