Search code examples
phphtmlcachinglocal-storagebrowser-cache

Is it possible to store image on user's browser


On my main page there is a big size image (~6 MB). It's kind of map.
I would like to store that image in user's browser even if they close browser or restart their PC, is it possible somehow? That image is used in CSS file if it matter.
My hosting has small bandwidth, so I would like to store that image as long as I can on user's browser Thanks for any tips.


Solution

  • There are a few options for this, some more convoluted than others.

    The most cross-browser way is to make sure you set your cache headers to long cache times, and keep the URL consistent (similar to @Petah's suggestion).

    Some browsers support the HTML5 File API. You could use JavaScript to store images here if it's supported.

    You could also use the HTML5 AppCache (also with limited support), but this means you have to be careful about how you structure your application - because the HTML page will be cached forever until the cache manifest changes.

    You can also serialise your image as a Data URL (fairly well supported) and store that string locally somehow (localstorage, cookies), but since cookies have small size-limits, and you wouldn't want to store 6MB in the HTML 'cos the page would take forever to load, this is probably not a great option.