gpt4 book ai didi

C# Excel 文件下载损坏文件?

转载 作者:行者123 更新时间:2023-12-04 22:04:41 26 4
gpt4 key购买 nike

我编写了一些代码,将某个文件下载到用户的计算机,这是一个 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();

有任何想法吗?

最佳答案

有时是编码问题

 var result = new StreamWriter(output); 

写入输出缓冲区流时不要使用任何特定的编码

关于C# Excel 文件下载损坏文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com