0

Application has to allow downloading of Excel files. The files are stored on server's hard disk, and returned using .NET File class (this should take care of content type headers). With xlsx it works fine. With xls on the client side i get the response of type "application/octet-stream", which results in broken excel file.

I tried same scenario on dev machine, it works fine (the local IIS returns correct "application/vnd.ms-excel"). I did some logging in the production, the MIME Type is getting identified correctly, so the return this.File(Path.Combine(pathDir, fileName), contentType, fileName); part is correct.

Since it works on my dev machine, i assume it is an IIS problem. The MIME Type settings for xls and xlsx files look identical on my dev and server machine.

The code for returning the File:

    // just some helper maching extension to MIME Type
    var contentType = IdentityFileType.GetFileType(fileName); 
    if (!string.IsNullOrEmpty(contentType))
    {
        var pathDir = Path.Combine(
            ConfigurationManager.AppSettings["ContentDir"], ConfigurationManager.AppSettings["FileDirRel"]);

        if (System.IO.File.Exists(Path.Combine(pathDir, fileName)))
        {
            // on log the contentType is "application/vnd.ms-excel", fileName "somefile.xls"
            return this.File(Path.Combine(pathDir, fileName), contentType, fileName);
        }
    }
    // else restore state and inform user

Any ideas why it would override the correctly set content type and return the default octet-stream ?

FwR
  • 1
  • 1
  • 1

1 Answers1

1

try modify your server web config

<system.webServer>
  <staticContent>
    <remove fileExtension="xls"/>
    <mimeMap fileExtension="xls" mimeType="application/vnd.ms-excel"/>
  </staticContent>
</system.webServer>
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • Added the suggested code to config, still get 'application/octet-stream'. – FwR Sep 21 '14 at 16:29
  • are your files on server static? Could the they be corrupted? You can try create custom respond without using MVC file method. – aleha_84 Sep 21 '14 at 17:01
  • Yes, they are static. I did check - they are fine. The problem only occurs with *.xls, which returns wrong content type and thus after saving on client machine the file becomes corrupted. I'll look into custom responses, although it seems like a bit overhead. – FwR Sep 21 '14 at 18:02
  • read [this](http://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc), maybe it helps. – aleha_84 Sep 21 '14 at 18:54
  • the File abstraction is straightforward, it does map to the correct class. I still believe the problem is somewhere in the IIS configurations. – FwR Sep 21 '14 at 20:38