C# ASP.NET 如何将文件流式传输给用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET How To Stream File To User
提问by RSolberg
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.
最初我试图弄清楚 Response.Close 和 Response.End 之间的区别,但在做了更多的谷歌搜索和研究之后,很明显我没有看到 Byte[] 被发送回客户端的常见方式。我将在下面留下代码示例,但我想知道这样做的行业标准是什么。
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();
采纳答案by Mitch Wheat
I wouldn't call Response.Close()
or Response.End()
.
我不会打电话Response.Close()
或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.End()
将在那时停止页面执行/渲染。不会Response.End()
运行以下代码。响应在该点终止,没有进一步的输出添加到流中。
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.Close()
类似于Response.End()
,但允许在调用后执行代码(但不能在页面响应中发送进一步的输出)。
Response.Flush()
will send any remaining response items to the page.
Response.Flush()
将任何剩余的响应项发送到页面。
From an IIS core team member:
来自IIS 核心团队成员:
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().
Response.Close 向客户端发送一个重置数据包,在错误情况以外的任何情况下使用它都会导致各种问题 - 例如,如果您与具有足够延迟的客户端通话,重置数据包可能会导致任何其他响应数据被缓冲在服务器、客户端或介于两者之间的某个地方被删除。
在这种特殊情况下,压缩涉及在响应中寻找常见模式,并且压缩代码必须缓冲一定数量的响应以增加找到更长重复模式的机会 - 一旦你做 Response.Close()。
简而言之,不要使用 Response.Close()。