MemoryStream返回类型写入Http

MemoryStream返回类型写入Http

本文介绍了是否可以将WCF MemoryStream返回类型写入Http Response对象以作为Excel文件下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I built a parsing application that reads xml files and populates an Excel workbook using the NPOI library. Originally, I had that as part of my .net web app and would get the MemoryStream from NPOI and write that to the Response object so the browser would download it. I've since moved the parsing to a WCF netTcp service hosted in a Windows Service. The communication works great, but when I return the MemoryStream from the WCF service and write it to the response, I get the following error:

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

My question is: What happens to the stream when it gets passed from the wcf service to my client? The stream is (theoretically) the exact same stream from NPOI that I was writing to the response originally. Is there any special processing that I need to do on the client to make this work?

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

Here is my client code: (the exception get thrown at 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以找到有关异常的更多详细信息).

You seem to retrun stream to request for partial page update with Update panel (search for Sys.WebForms.PageRequestManagerParserErrorException to find more details about exception).

请确保仅将流重定向到整页请求(浏览器本身发出的GET/POST,而不是期望某些特定类型的响应的页面上的某些脚本发出的GET/POST).

Make sure that you are retruning stream only to full page request (GET/POST issued by browser itself, not by some script on the page that expects some particular type of responce).

这篇关于是否可以将WCF MemoryStream返回类型写入Http Response对象以作为Excel文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:48