33

I am writing a small application which serves images from the local computer, so they can be accessed as http://localhost:12345/something/something (which returns a jpeg).

How can I force the browser to cache this, so only a single request would be sent to the server. Would this header be sufficient

HTTP/1.1 200 OK
Cache-Control: public, max-age=99936000
Content-Length: 123456
Content-Type: image/jpeg

This seems to work with Firefox 3.x but would it be sufficient globally for other browser as well?

The Unknown
  • 17,346
  • 29
  • 69
  • 91
  • Related: How to force a web browser NOT to cache images. http://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images – The Unknown Aug 16 '09 at 21:13

3 Answers3

17

A Last-Modified and Expires header might also be useful additions.

Your server should also check for requests featuring an If-Modified-Since header, and return a 304 Not Modified response if possible to speed things along.

Paul Dixon
  • 277,937
  • 48
  • 303
  • 335
0

try the expires http header: http://en.wikipedia.org/wiki/List_of_HTTP_headers

You probably have to merge this with your cache-control

Niko
  • 5,753
  • 2
  • 33
  • 48
-6

You can't and nor should you - this is the user's decision to make not yours.

What you can do is strongly suggest with the right headers, but even then you should expect t deal with a number of HEAD requests ever after.

annakata
  • 70,224
  • 16
  • 111
  • 179
  • 3
    In some cases where bandwidth are an issue you should. – EaterOfCode Mar 06 '14 at 11:02
  • 22
    Responses where the responder suggests that you shouldn't be doing something, rather than answering the question.. are the most obnoxious. Add to that, that this is actually a reasonable question. – whitehawk Oct 19 '14 at 18:34
  • 4
    Try saying that when you have a gallery with 4.1 million images with links being shared all over different websites. – Captain Hypertext May 23 '16 at 01:25