Image caching from the browser

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

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 …