ASP.NET如何将文件流传给用户 [英] ASP.NET How To Stream File To User

查看:95
本文介绍了ASP.NET如何将文件流传给用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我试图找出Response.Close和Response.End之间的区别,但是在进行更多的搜索和研究之后,很清楚,我没有看到一个通用的方式Byte []被发送回客户端。我将在下面留下代码示例,但我想知道行业标准是这样做的。

 字节[ ] myBytes = GetReportBytes(); 
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader(content-length,myBytes.Length.ToString());
HttpContext.Current.Response.AppendHeader(content-Disposition,attachment; filename =+ this.ReportFileName + GetReportExtension());
HttpContext.Current.Response.ContentType = GetApplicationContentType();
HttpContext.Current.Response.BinaryWrite(myBytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
// CERT FIX
//HttpContext.Current.Response.End();


解决方案

我不会调用 Response.Close() Response.End()



Response.End()将停止页面执行/渲染。将运行 Response.End()之后的代码。此时,响应终止,没有进一步的输出添加到流。



Response.Close()类似于 Response.End(),但允许代码在调用后执行(但是在页面响应中不能再发送输出)。



Response.Flush()将发送任何剩余的响应项目到页面。



IIS核心团队成员


Response.Close将客户端的重置数据包发送到
,并将其用于任何
除了错误条件之外,还会导致
的各种问题 - 例如,如果
正在与具有足够的
延迟的客户端通话,则重置数据包可能导致
任何其他响应数据缓冲在
上的服务器,客户端或
中的某个地方要被删除。



在这种特殊情况下,压缩
涉及查找响应中的共同模式
和一些
响应必须由
压缩代码缓冲,以增加
找到更长重复
模式的机会 - 这部分缓冲
无法发送到客户端
做Response.Close()。



总之,不要使用Response.Close ()。



Initially I was trying to figure out what the difference is between Response.Close and Response.End, but after doing more googling and research, its clear that I haven't seen a common way a Byte[] gets sent back to the client. I'll leave the code sample below, but I would like to know what the industry standard is for doing this.

Byte[] myBytes = GetReportBytes();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader("content-length", myBytes.Length.ToString());
HttpContext.Current.Response.AppendHeader("content-Disposition", "attachment;filename=" + this.ReportFileName + GetReportExtension());
HttpContext.Current.Response.ContentType = GetApplicationContentType();
HttpContext.Current.Response.BinaryWrite(myBytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
//CERT FIX
//HttpContext.Current.Response.End();

解决方案

I wouldn't call Response.Close() or Response.End().

Response.End() will stop the page execution/rendering at that point. No code following Response.End() will be run. The response is terminated at that point with no further output added to the stream.

Response.Close() is similar to Response.End(), but allows code to be executed after it is called (but no further output can be sent in the page response).

Response.Flush() will send any remaining response items to the page.

From an IIS core team member:

Response.Close sends a reset packet to the client and using it in anything other than error condition will lead to all sorts of problems - eg, if you are talking to a client with enough latency, the reset packet can cause any other response data buffered on the server, client or somewhere in between to be dropped.

In this particular case, compression involves looking for common patterns within the response and some amount of response has to be buffered by the compression code to increase the chance of finding longer repeating patterns - this part that is buffered cannot be sent to the client once you do Response.Close().

In short, do not use Response.Close().

这篇关于ASP.NET如何将文件流传给用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆