当我们要想要将一个大文件推给(http)客户端时,由于文件太大我们需要分块传输,下面举例说明

using (var ms = new MemeryStream())
{
    //100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
    const long ChunkSize = 102400;
    byte[] buffer = new byte[ChunkSize];
 
    //获取下载的文件总大小
    long dataLengthToRead = ms.Length;
    while (dataLengthToRead > 0 && Response.IsClientConnected)
    {
        //读取的大小
        int lengthRead = ms.Read(buffer, 0, Convert.ToInt32(ChunkSize));
                //向http流写入数据
        Response.OutputStream.Write(buffer, 0, lengthRead);
        Response.Flush();
        dataLengthToRead = dataLengthToRead - lengthRead;
    }
}