我已经写了一些代码,下载某个文件到用户的计算机,这是一个MS Excel .xlsx文件。问题是,当用户从我的web应用程序下载并打开文件时,我在excel中收到一条消息:“我们发现file.xlsx中的某些内容存在问题。”然后我点击是恢复的内容,这是日志文件显示:C#Excel文件下载破坏文件?
<?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的正确模式。另外,当我在文件夹资源管理器中打开该文件时,仅在下载后才会出现该问题,。我已经尝试了谷歌浏览器和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();
任何想法?
正如我所说,在文件资源管理器中,打开文件自然不会给出任何错误。一旦通过程序下载,该文件就有错误。如果你可以用FileResult显示我的方式,我可以将你的答案标记为正确的? – 2014-10-19 13:06:41
嘿,对不起,迟交回复。我增强了答案,让我知道它是否有帮助。 – Mzn 2014-10-23 05:26:04