【发布时间】:2014-10-19 12:03:48
【问题描述】:
我编写了一些代码,将某个文件下载到用户的计算机上,这是一个 MS Excel .xlsx 文件。问题是,当用户从我的网络应用程序下载并打开文件时,我在 excel 中收到一条消息,说“我们发现 file.xlsx 中的某些内容存在问题”。然后我单击是以恢复内容,这就是日志文件显示的内容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error128120_01.xml</logFileName><summary>Errors were detected in file 'F:\Downloads\file.xlsx'</summary><additionalInfo><info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info></additionalInfo></recoveryLog>
这是我用来下载 C# 文件的代码。根据我的研究,我相信这是 .xlsx 的正确 mimetype。此外,当我在文件夹资源管理器中打开文件时,仅在下载后不会出现问题。我已尝试过 Google Chrome 和 Internet Explorer。
var fileInfo = new FileInfo(Path.Combine(Server.MapPath("~/Content/"), "file.xlsx"));
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=" + "file.xlsx");
Response.TransmitFile(fileInfo.FullName);
Response.Flush();
有什么想法吗?
【问题讨论】:
标签: c# asp.net-mvc excel httpresponse