# Image caching from the browser

**URL:** <https://www.ruby-forum.com/t/image-caching-from-the-browser/111939>\
**Category:** Rails\
**Created:** [August 13, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/image-caching-from-the-browser/111939 "2007-08-13T17:40:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzybuddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jazzybuddy](https://www.ruby-forum.com/u/jazzybuddy)\
**Post date:** [August 13, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/image-caching-from-the-browser/111939/1 "2007-08-13T17:40:50Z")

</div>

Hello everyone,

```
I am struck with the problem of retrieving an image from database. I

```

am using

\<%= form\_tag\_with\_upload\_progress({:action =\> ‘update\_plugin\_content’},  
{ :begin =\> “new Effect.Appear(‘loading’);”,  
:finish =\> “do\_finish(arguments[0]);new  
Effect.Fade(‘loading’);”,  
:name =\> “message\_form”}  
)%\>

(This form\_tag\_with\_upload\_progress is a method of uploading image  
thru AJAX)

for uploading the image into the database. Itz working properly.  
After uploading I am doing an Ajax.Updater for getting the new image  
from the database.  
Here the problem is the image is getting fetched from the browser cache.

I have implemented the following to try to solve this problem.

1. I had added a random number to the URL so that every request that  
goes is a new URL and

var url =  
‘/preference/get\_user\_logo?plugin\_preferences\_id=’+plugin\_preferences\_id+“&”+new  
Date().getTime()+“=”+new Date().getTime();

new Ajax.Updater(‘uploaded\_logo’,url, {asynchronous:true,  
evalScripts:true,onLoading:function(){replace\_logo\_with\_loading(‘uploaded\_logo’,‘ipolipo\_loading’);},onComplete:function(request){replace\_logo\_with\_loading(‘ipolipo\_loading’,‘uploaded\_logo’);get\_response();}});return  
true;

This did not work.

1. My friend who works in java had suggested to set the headers  
cache-control to “no-cache”

so the full action became

def get\_user\_logo  
plugin\_preference\_id = params[:plugin\_preferences\_id]  
user\_logo = PluginPreference.find(plugin\_preference\_id)

image\_of\_user\_logo = user\_logo.image

@response.headers[“Expires”] = 0  
@response.headers[“Pragma”] = “no-cache”  
@response.headers[“Cache-Control”] = “no-cache”

if user\_logo.image == nil or image\_of\_user\_logo.empty?  
#… code send a default image  
else  
send\_data user\_logo.image, :type =\> “image/png”, :disposition =\>  
“inline”  
end  
end

But this also did not work.

1. just as suggested in the rails CookBook

[http://manuals.rubyonrails.com/read/chapter/62](http://manuals.rubyonrails.com/read/chapter/62)

def image  
@photo = Photo.find(@params[‘id’])  
minTime = Time.rfc2822(@request.env[“HTTP\_IF\_MODIFIED\_SINCE”])  
rescue nil  
if minTime and @photo.updated\_at \<= minTime  
# use cached version  
render\_text ‘’, ‘304 Not Modified’  
else  
# send image  
@response.headers[‘Content-Description’] = @photo.description  
@response.headers[‘Last-Modified’] = @photo.updated\_at.httpdate  
send\_data @photo.data, :type =\> @photo.mimeType, :disposition =\>  
‘inline’  
end  
end

I had modified my action like that suggested.

But the “Time.rfc2822(@request.env[“HTTP\_IF\_MODIFIED\_SINCE”]” comes as  
nil and  
“@photo.description” is not recognized i.e., not proceeding further and  
also not throwing exception.

Is the “@request.env[“HTTP\_IF\_MODIFIED\_SINCE”]” needs to be set  
anywhere. If so where it has to be set?

The problem is clearly with the browser cache. But I am unable to find  
the solution. Please help me in this .  
Thanks in advance…

Regards,  
JazzyBuddy …
