gpt4 book ai didi

c# - 可以将 WCF MemoryStream 返回类型写入 Http Response 对象以作为 Excel 文件下载吗?

转载 作者:行者123 更新时间:2023-11-30 22:42:06 28 4
gpt4 key购买 nike

我构建了一个解析应用程序,它使用 NPOI 读取 xml 文件并填充 Excel 工作簿。图书馆。最初,我将其作为我的 .net Web 应用程序的一部分,并从 NPOI 获取 MemoryStream 并将其写入 Response 对象,以便浏览器下载它。我已经将解析移动到 Windows 服务中托管的 WCF netTcp 服务。通信效果很好,但是当我从 WCF 服务返回 MemoryStream 并将其写入响应时,出现以下错误:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

我的问题是:当流从 wcf 服务传递到我的客户端时会发生什么情况?该流(理论上)与我最初写入响应的来自 NPOI 的流完全相同。是否需要在客户端上执行任何特殊处理才能完成这项工作?

这是我的客户端代码:(在 Response.End() 处抛出异常

string filename = "test.xls";

ms = client.GetExportedFile();
byte[] b = ms.ToArray();

ms.Flush();
ms.Close();

Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader( "Content-Disposition", string.Format( "attachment;filename={0}", filename ) );
Response.AddHeader( "Content-Length", b.Length.ToString() );
Response.BinaryWrite( b );
Response.End();

最佳答案

您似乎重新运行流以使用更新面板请求部分页面更新(搜索 Sys.WebForms.PageRequestManagerParserErrorException 以查找有关异常的更多详细信息)。

确保您仅针对完整页面请求(浏览器本身发出的 GET/POST,而不是页面上需要某种特定类型响应的脚本)重新调整流。

关于c# - 可以将 WCF MemoryStream 返回类型写入 Http Response 对象以作为 Excel 文件下载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4595341/

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