0

We have some users who when they open an excel file on my website it takes ages. If they simpely Save as and then open it's quick. They have the same issue on other sites so it's a problem on their side. However is there a way to force the browser to only offer save and not offer open?

As noted here: Is there a way to force the user to download a file from a href link rather than to open it in a browser window? and here: Forcing to download a file using PHP I'm currently using:

 Response.ContentType = "application/ms-excel";
 Response.AddHeader("content-disposition", "attachment; filename=Report.xlsx");
 Response.AddHeader("Content-Length", new System.IO.FileInfo(fileName).Length.ToString());

But this is still offering the open option.

Is there a way to force this or is it simply dependent on the users browser settings.

Community
  • 1
  • 1
jimmy
  • 709
  • 3
  • 15
  • 33
  • No, you cannot change the browser's 'save as' dialog. At most you can provide HINTS via the content-disposition, but you cannot do anything to disable client-side functionality. – Marc B Aug 15 '12 at 16:38

2 Answers2

2

Just send a different Content-Type (application/octet-stream):

Response.ContentType = "application/octet-stream";

This way the browser doesn't recognize the format and just proposes to save the file.

--- EDIT ----

Today, I got to know another header field, that probably fixes your problem:

"X-Content-Type-Options: nosniff"

Description: "The only defined value, 'nosniff', prevents Internet Explorer from MIME-sniffing a response away from the declared content-type. This also applies to Google Chrome, when downloading extensions.[31]"

(http://en.wikipedia.org/wiki/List_of_HTTP_header_fields)

SDwarfs
  • 3,189
  • 5
  • 31
  • 53
  • I tried that, even tried octessst-streasssm, but it still says this is a excel file do you want to open. When I change the name to .zxlsx it then said, unknown file type, save or browse... – jimmy Aug 15 '12 at 17:10
  • Yeah, the browser tries to find out the MIME-Type using the file extension. Maybe try: "application/x-download" -- let me know if it worked. Thx! – SDwarfs Aug 15 '12 at 17:25
  • nope, didn't help either ... I think the problem is the browsers are too clever for there own good. – jimmy Aug 15 '12 at 17:53
  • Ok, one more try: "application/x-force-download" <-- what about this? – SDwarfs Aug 15 '12 at 23:13
  • Ok, now I'm nearly out of ideas... You could have a look at tools.ietf.org/html/rfc6266 [RFC6266] to check weather your Content-Disposition header fields are all correct. Especially the parameter "filename" is a bit tricky. Maybe the header is ignored if its not correct low/uppercase... or the filename isn't encoded correctly; But maybe there is no working solution except for proposing other user interaction as: "right-click + save as" or "shift + click" (or was it ctrl + click) on the download link. – SDwarfs Aug 17 '12 at 08:42
  • PS: Try some really simple file name as "test.xlsx"; Does your download link end with ".xlsx" too? If so, try to change it to something else as just the ID ... Next question: Which browser(s) are you trying? – SDwarfs Aug 17 '12 at 08:46
  • I'm using Firefox and IE9, I put up a not saying if they have issues to select save rather then open. Looks like I'll just have to leave it at that – jimmy Aug 18 '12 at 11:18
0

Although the Question is quite old I am going to answer...

For IE8/9 and Chrome this worked for me:

Response.AddHeader("X-Download-Options", "noopen");
pastrami01
  • 341
  • 4
  • 9