加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET如何将文件流式传输给用户

发布时间:2020-12-15 18:20:14 所属栏目:百科 来源:网络整理
导读:最初我试图找出Response.Close和Response.End之间的区别,但是在进行了更多的谷歌搜索和研究后,很明显我没有看到Byte []被发送回客户端的常见方式.我将在下面留下代码示例,但我想知道行业标准是做什么的. Byte[] myBytes = GetReportBytes();HttpContext.Curr
最初我试图找出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();

解决方法

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

Response.End()将在此时停止页面执行/呈现.将不会运行Response.End()之后的代码.响应在该点终止,没有进一步的输出添加到流.

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

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

从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().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读