【发布时间】:2014-11-15 13:04:40
【问题描述】:
应用程序必须允许下载 Excel 文件。 这些文件存储在服务器的硬盘上,并使用 .NET File 类返回(这应该处理内容类型标头)。使用 xlsx 它可以正常工作。在客户端使用 xls 时,我得到类型为“application/octet-stream”的响应,这会导致 excel 文件损坏。
我在开发机器上尝试了相同的场景,它工作正常(本地 IIS 返回正确的“application/vnd.ms-excel”)。我在生产中做了一些记录,MIME 类型被正确识别,所以 return this.File(Path.Combine(pathDir, fileName), contentType, fileName); 部分是正确的。 p>
由于它可以在我的开发机器上运行,我认为这是一个 IIS 问题。 xls 和 xlsx 文件的 MIME 类型设置在我的开发和服务器计算机上看起来相同。
文件返回代码:
// 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
知道为什么它会覆盖正确设置的内容类型并返回默认的八位字节流吗?
【问题讨论】:
标签: asp.net-mvc excel iis mime-types