Cache Control Private

Ugh! It is getting harder and harder to find the right answer to technical problems on the first page of Google’s search results. I offer this here so I never have to search for the answer again.

When you are developing web applications, you sometimes want the user to be presented with a “File Save” dialog box to save a file on their computer instead of opening it in their browser.

To do this you add a “Content-Type” and a “Content-Disposition” header. For an Excel file in a Ruby on Rails application you would put these lines in the controller:

def download @headers[‘Content-Type’] = “application/vnd.msexcel” @headers[‘Content-Disposition’] = “attachment;filename=myfile.xls” end

If you do this Mozilla, Firefox, and Internet Explorer will download the file and save it to your desktop. If you click “Open” instead of “Save” in Mozilla or Firefox, Excel will open the file instead. Just as it should.

If you click “Open” instead of “Save” in Internet Explorer, Excel will complain that it can’t find the file. Unless Excel is already open. If Excel is already open the file will open normally just as it does in Mozilla or Firefox.

After 15 or so minutes of searching I was finally able to find the right search terms to illuminate the problem. Internet Explorer has some cache issues. To fix this, add a “Cache-Control” = “private” header.

The method in Ruby on Rails would become:

def download @headers[‘Content-Type’] = “application/vnd.msexcel” @headers[‘Content-Disposition’] = “attachment;filename=myfile.xls” @headers[‘Cache-Control’] = “private” end

I hope to never have to search for this again.

Comments are closed.