Greg
Greg

Reputation: 29

ASP.NET - Window.Open(URL) - File is cached, how do I stop it?

I'm using ASP.NET to load PDFs. The files are called by a simple JavaScript in the code behind:

ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", "window.open('" + url + "')", true);

Is there a way to prevent IE from caching these files? Maybe something to add to the endo f the JS to prevent caching? Some files are modified based on user input, so the same file name may be a completely different file the next time the same URL is loaded.

I've tried deleting the file before it is modified and saved, but that did not help. The only thing that helped was a manual refresh of the IE window once the PDF had loaded into it.

Thanks!

Upvotes: 0

Views: 4762

Answers (3)

Quintin Robinson
Quintin Robinson

Reputation: 82375

A common convention to avoid this problem with IE caching issues (if you don't want to modify the cache policy) is to append a random number (generated on the fly) to the end of the url as a querystring parameter. This does not have to be read on the server, it just helps uniquely differentiate the url for the request.

ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", "window.open('" + url + "?random=' + Math.random())", true);

Upvotes: 1

hungryMind
hungryMind

Reputation: 7009

Response.Cache.SetExpires(-1);

on asp.net page

Upvotes: 1

aepheus
aepheus

Reputation: 8197

Attach a random number to the querystring of the url. A date stamp is often used:

"window.open('"+url+"&_='+(new Date()).valueOf())"

Which will ensure that a cached copy is not retrieved (cache varies on querystring).

Upvotes: 1

Related Questions