Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Caching 1

Status
Not open for further replies.

Laeg

Programmer
Nov 29, 2004
95
IE

I have a form that allows a user to upload an image file. The last image to be uploaded is visible on this form. This image file is renamed to the same name everytime the upload occurs and is placed in the same directory. So for example a user can upload any image but it will always be renamed to "mypic.jpg" overriding the previous image and dumped into a "/upload" directory. The problem is that when a new image is uploaded, because it has the same name as the old image, the old image is still displayed due to caching after a new image has been uploaded. If I hit refresh it will then display the latest image. I am using ASP as my server side language and have included its non-caching code :


<% Response.CacheControl = "no-cache" %>
<% Response.AddHeader "Pragma", "no-cache" %>
<% Response.Expires = -1 %>


and I have also added the HTML non caching code :


<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">


These have no effect, is there anyway I can force the browser to get the latest version of the image?
 

Append a random parameter to the image filename:

Code:
mypic.jpg?randNum=123456

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Unfortunately I cannot append a random number to the filename, the filename is set.
 

Just to clarify what I said... I didn't mean that you should rename the file, in case that is what you thought I meant.

What I meant was that you should change your source code to add the random number parameter, which would fool most browsers into thinking the file was a different file, and getting a new version of it.

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I don't follow, if I don't rename the file and it sits on disk called "mypic.jpg" how does appending a random number to my source code access this file?
 

Because the parameter is not part of the filename. Did you try it yet?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Here you go - from the official HTTP 1.1 specs:

Code:
3.2.2 http URL

   The "http" scheme is used to locate network resources via the HTTP
   protocol. This section defines the scheme-specific syntax and
   semantics for http URLs.

   http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]

   If the port is empty or not given, port 80 is assumed. The semantics
   are that the identified resource is located at the server listening
   for TCP connections on that port of that host, and the Request-URI
   for the resource is abs_path (section 5.1.2). The use of IP addresses
   in URLs SHOULD be avoided whenever possible (see RFC 1900 [24]). If
   the abs_path is not present in the URL, it MUST be given as "/" when
   used as a Request-URI for a resource (section 5.1.2). If a proxy
   receives a host name which is not a fully qualified domain name, it
   MAY add its domain to the host name it received. If a proxy receives
   a fully qualified domain name, the proxy MUST NOT change the host
   name.

Basically, anything after the "?" is considered a query, and does not affect the path portion of the filename.

Hope this clarifies things,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
That, my friend, is neat. Cheers.
 

It's one of those little gems to tuck away for a rainy day - it's got me out of a few holes ;o)

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I frequently get the date/time as a number and append that to my URLs to prevent caching. It does seem to work.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top