0

I'm downloading a file with Response.WriteFile and appending a timestamp to the filename in order to prevent IE from caching the downloaded file. But the browser is changing ? to _.

string filePath = Server.MapPath("~/test.docx");
string fileName = string.Concat("test.docx", "?t=", DateTime.Now.Ticks);
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.WriteFile(filePath);
...

Expected result: test.docx

Actual result: test.docx_t=635823369853307954

Is this possible?

Kristoffer Jälén
  • 3,223
  • 3
  • 21
  • 39

1 Answers1

1

Browser will eventually cache result according to requested url (and other headers), not to filename attribute in "content-disposition" response header.

The latter is known to the brower only after he decided to call again the web server and got a response: if it has already decided to use the cache, it will not send any new request to server.

Gian Paolo
  • 3,802
  • 4
  • 13
  • 29
  • So my approach is wrong? I should use [this solution](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers/2068407#2068407) instead? – Kristoffer Jälén Nov 05 '15 at 15:43
  • 1
    Yes, your approach is wrong That solution seems correct. but keep in mind that you actually have to tell the browser not to cache that content on the "previous" request of the file, settings correct headers. This ends in setting the no-cache headers in every response you serve for that url. By the way, filename attribute in content disposition header is just an hint for the browser: "when the user download the file, suggest him this as file name to save file on disk." – Gian Paolo Nov 05 '15 at 15:52