5

The problem

I am working on an intranet, and have some problems with documents that IE lets the local Office installation open.

I am serving the users the files with the following anchor tag:

<a target="_blank" download="{{fileName}}" href="{{filePath}}">

When a user clicks this link, IE gives the user the decision to either open or save it. If the user chooses to open the file, and then decides that he/she wants to edit the file, they click "enable edit" in the top of the office application. This gives the user an error, as the Office application is unable to connect to the site and get the file the user want to edit.

Office Application Name cannot connect to (my file)...

What I want to do, is to let users edit a copy of the document they find online. A copy they then can save, and re-upload.

I think one of the problems is that the files are protected by a login. And maybe also that the intranet is running on SSL.

Research

Researching for solutions to the problem, I found this support page from Microsoft talking about something like what I am experiencing. In their error message three, they are writing:

Office Application Name cannot open the file.

In my case, I am getting the message:

Office Application Name cannot connect to (my file)...

It is almost the same, and Microsoft writes that one problem could be that:

When Internet Explorer communicates with a secure Web site through SSL, Internet Explorer enforces any no-cache request. If the header or headers are present, Internet Explorer does not cache the file. Consequently, Office cannot open the file.

This got me thinking, and I begun researching for the problem, finding StackOverflow pages like "How to configure static content cache per folder..." and "Leverage browser caching in IIS". I also checked The IIS.net Config Reference to see if there was anything here that could help me in my case.

I think the main problem is that when opening the file, it is not saved locally, and therefore the Office applications cannot edit it. As of now, I think the way to overcome this problem is to remove the no-cache header, and instead use some sort of short caching for the file so that it is forced to be downloaded by the browser.

What I have tried

Cache-Control

I tried the techniques from the two previous SO posts linked, trying to set up caching for static files in web.config, but forcing caching doesn't change anything (I used this technique to make sure the cache-control was right when I asked for a file). The office application still tries to open the file from a webpage that it doesn't have access to.

Content-Disposition

Another thing I tried was to force internet explorer to download the file. Completely removing the option to open it. Microsoft also has a support page about it. But it didn't seem to work. In Global.asax I tried the following code (just to see if I could get word files to work):

<script runat="server">
    void Application_PreSendRequestHeaders(Object sender, EventArgs e) {
        Response.Headers.Set("Content-Type", "application/ms-word");
        Response.Headers.Set("Content-Disposition", "attachment; filename=fname.ext");
    }
</script>

But that only made Internet Explorer try to download all pages I tried to access.

Disabling authentication control on files

We always check to see if a user does in fact have access to the file requested. We don't want people to snoop around others files. Disabling this check resulted in all of the Office applications being able to open the file when a user wanted to edit a file. But this is a terrible solution, and just delivers a bunch of security problems.

The Question

So my question is, "how do I enable editing in an office document that IE has opened from a login protected intranet?" If it is something about the cache, then how do I get around this? Is there some way that I can tell IE to save a local copy when a users chooses to "open" a file, and how can I tell the Office application to look for this locally cached version of the file instead of trying to download it from the server?

Update

I narrowed down the problem to being authentication of the office application. When I click the "enable edit" button in my Office application, the application is trying to download the file, so that it can edit the file. This request is then rejected by the server (serving a 404), as the Office application is not logged in, and does not have any kind of cookie to tell the server that is does in fact have access to that file. This results in the user getting a message, informing the user that it was not possible to open the requested file.

2 Answers 2

2

You should be able to set the headers on individual calls so that files can be downloaded. If you are using MVC, the call return Controller.File(), there is a fileDownloadName option that takes care of setting the attachment; filename=... header so that it forces it to download.

Here's the Action (using MVC):

public FileResult Download() {
  ...
  return File(filename, mimetype, fileDownloadName)
}

If you are not using MVC, use this call only when serving the file:

Response.Headers.Set("Content-Disposition", "attachment; filename=" + fileDownloadName);

BTW, This was a very thorough explanation of the problem!

2
  • Thank you for your answer. I'm not really able to verify the answer, as I ended up using WebDav for the problem. Thanks for praising my question, I myself hate short questions so tried to make it good ;)
    – Squazz
    Dec 7, 2016 at 22:20
  • 1
    I can verify it works. Had almost the same problem as OP (but no SSL and it didn't even work on localhost) For some reason our download action returned new FileStreamResult, which is actually what the controllers File method returns, but with one significant difference: File provides the possibility to define the filename (see bradlis7's example) and that's what makes it work (tested in IE11)
    – Arikael
    Jan 25, 2017 at 7:05
2

When a user chooses to "open" instead of save, what IE does is save a copy of the file to the temp folder and then have the appropriate application open it. When closed, IE may or may not then delete the temp folder file.

When a user chooses to "enable editing" on a file from a non-local network, Offices closes the file, then reopens it in edit mode.

So your sequence of events is: IE saves file to temp. Office opens it. User clicks enable edit. Office closes it. IE deletes it. Office can't find it to reopen.

You could try NOT setting the content header, but IE may deduce the file type from the file extension anyway. Another option would be to educate the user. Include instructions telling them to save, and then open the saved copy. I generally approach the web from the assumption I have no control over what happens with the data I send my users. With the wide range of browsers and office applications out there, it's really a losing battle.

2
  • But, isn't there any way where I force IE & Office to not do this? Some way that I can tell Office that "the file you are opening, cannot be fetched again"?
    – Squazz
    Apr 27, 2016 at 7:40
  • @Squazz I think your best bet may be to get in contact with your network admins and see if they're willing to use LDAP/Active Directory settings to force users to always save Excel files. That's really the only viable path I see. Apr 30, 2016 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.