Reputation: 1
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 ?
Upvotes: 0
Views: 3292
Reputation: 8539
try modify your server web config
<system.webServer>
<staticContent>
<remove fileExtension="xls"/>
<mimeMap fileExtension="xls" mimeType="application/vnd.ms-excel"/>
</staticContent>
</system.webServer>
Upvotes: 1