【发布时间】:2013-12-02 06:02:08
【问题描述】:
问题是所有支持的文件都可以正常工作(jpg、gif、png、pdf、doc 等),但是 .docx 文件在我下载时显示已损坏,需要 Office 修复它们才能正常使用打开。
这是我的代码:
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
Response.BinaryWrite(RptBytes);
stream.Dispose();
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
我从其他网站得到了解决方案,
即
事实证明,docx 格式需要在 Response.BinaryWrite 之后有 Response.End() 而不是 HttpContext.Current.ApplicationInstance.CompleteRequest()
Why are .docx files being corrupted when downloading from an ASP.NET page?
它工作正常,但 Response.End 在线程被中止时抛出异常。
【问题讨论】:
-
我猜你的所有下载都已损坏,除了某些文件格式比其他文件格式更宽容。显示图像的软件可能不会注意到 jpg 或 gif 上的错误字节。但是,docx 文件实际上是 zip 文件,对损坏的字节更加严格。我要做的是:1. 将文件流读入字节数组,2. 关闭/处理您的流,然后通过 BinaryWrite 将字节数组写入响应
-
@Walter Stabosz 我想这可以写成答案而不是评论......