ASP.NET如何流文件至用户 [英] ASP.NET How To Stream File To User
问题描述
起初,我试图找出有什么区别Response.Close和到Response.End之间,但这样做更多的谷歌搜索和研究之后,它清楚地表明我还没有看到一个byte []被发回的常用方法客户端。我将离开code样品下方,但我想知道的行业标准是这样做的。
字节[] = myBytes GetReportBytes();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader(内容长度,myBytes.Length.ToString());
HttpContext.Current.Response.AppendHeader(内容处置,附件;文件名=+ 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()
将停止在页面执行/渲染在这一点上。没有code以下到Response.End()
将运行。的响应在该点终止,没有进一步的输出添加到流
Response.Close()
类似于到Response.End()
,但允许code到它被称为(但没有进一步的输出可在寻呼响应被发送)之后执行。
Response.Flush()
将发送任何剩余的答题页面。
从 IIS核心团队成员:
Response.Close发送一个复位包
用它在任何客户端和
除了错误条件会导致
以种种问题 - 例如,如果你
在谈论到客户端有足够的
延迟,复位分组可能导致
任何其他响应数据缓存上
服务器,客户端或在某处
之间被丢弃。
在这种特殊情况下,COM pression
包括寻找共同的模式
响应和一定量的内
响应必须由被缓冲
COM pression code键增强
发现不再重复几率
图案 - 这一部分缓冲
不能被发送到客户端,一旦你
做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()
orResponse.End()
.
Response.End()
will stop the page execution/rendering at that point. No code followingResponse.End()
will be run. The response is terminated at that point with no further output added to the stream.
Response.Close()
is similar toResponse.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屋!