zoukankan      html  css  js  c++  java
  • response.Close、response.End、response.Flush区别

    今天在做文件下载功能用到的是response的方法,首先我们要了解这些方法的作用。

    1.response.write():将信息写入http响应输出流。

    2.response.Flush:向客户端发送当前所有缓冲的输出

    3.response.end:将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发EndRequest事件。

    4.response.Close:关闭到客户端的套接字连接。

    看到这里,我们应该可以想到response实现下载的基本思想:

    1.将我们要下载的文件信息写入Http的响应输出流(response.write)

    下面我们就可以向客户端输出了,但是要注意在此会有2种方法:1.response.Flush 2.response.end

    1.我们使用response.Flush()方法时,就要多一步动作就是关闭/停止输出,为什么要做关闭/停止输出?因为不做这一步就会把整个页面的信息全部输出了,这个不是我们所需要的,所以可以使用【response.End网上都建议用它】或【reponse.Close这个方法的用法我在网上搜了好久都说尽量不用它】

    2.使用response.End方法时,我们可以不使用response.Flush的方法了。

    注意:上面所说的是我本地调试通过的两种方法

    1.使用Flush和Close可以通过并且没有报错的

    2.使用End方法也是可以的

    3.使用Flush和End我感觉多余但是也是可行的

    此文是自己总结,如有错误还望高手指出,大家共同学习

                  string l_strFileName = "FileName";
                    byte[] l_bytFileData = 把文件转换成Byte类型;
                    HttpContext.Current.Response.Clear();
                    l_strFileName = System.Web.HttpUtility.UrlEncode(l_strFileName);
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + l_strFileName);
                    if (l_bytFileData.Length == 0)
                    {
                        l_bytFileData = System.Text.Encoding.Unicode.GetBytes(" ");
                    }
                    HttpContext.Current.Response.BinaryWrite(l_bytFileData);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
    

      

  • 相关阅读:
    ExtJs系列教程
    linux 服务器时间 timedatectl命令时间时区操作详解
    aws CloudWatch Events
    AWS Shield
    aws ssm指令
    failed to set bridge addr: "cni0" already has an IP address different from 10.244.0.1/24
    AWS Systems Manager
    Amazon Inspector
    AWS 安全培训
    Amazon Inspector
  • 原文地址:https://www.cnblogs.com/WarBlog/p/5264542.html
Copyright © 2011-2022 走看看