四种下载文件方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


using System.IO;


/// <summary>
/// 文件下载有以下四种方式, 大文件下载的处理方法:将文件分块下载。
/// Response.OutputStream.Write
/// Response.TransmitFile
/// Response.WriteFile
/// Response.BinaryWrite
/// </summary>
public class DownHelper
{
    HttpResponse Response = null;
    public DownHelper()
    {
        Response = HttpContext.Current.Response;
    }


    public void DownloadByOutputStreamBlock(System.IO.Stream stream, string fileName)
    {
        using (stream)
        {
            //将流的位置设置到开始位置。
            stream.Position = 0;
            //块大小
            long ChunkSize = 102400;
            //建立100k的缓冲区
            byte[] buffer = new byte[ChunkSize];
            //已读字节数
            long dataLengthToRead = stream.Length;


            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition",
                string.Format("attachment; filename={0}", HttpUtility.UrlPathEncode(fileName)));


            while (dataLengthToRead > 0 && Response.IsClientConnected)
            {
                int lengthRead = stream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                Response.OutputStream.Write(buffer, 0, lengthRead);
                Response.Flush();
                Response.Clear();
                dataLengthToRead -= lengthRead;
            }
            Response.Close();
        }
    }


    public void DownloadByTransmitFile(string filePath, string fielName)
    {
        FileInfo info = new FileInfo(filePath);
        long fileSize = info.Length;
        Response.Clear();
        Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader("Content-Disposition",
            string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fielName)));
        //不指明Content-Length用Flush的话不会显示下载进度  
        Response.AddHeader("Content-Length", fileSize.ToString());
        Response.TransmitFile(filePath, 0, fileSize);
        Response.Flush();
        Response.Close();
    }


    public void DownloadByWriteFile(string filePath, string fileName)
    {
        FileInfo info = new FileInfo(filePath);
        long fileSize = info.Length;
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition",
            string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));


        //指定文件大小  
        Response.AddHeader("Content-Length", fileSize.ToString());
        Response.WriteFile(filePath, 0, fileSize);
        Response.Flush();
        Response.Close();
    }


    public void DownloadByOutputStreamBlock(string filePath, string fileName)
    {
        using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            //指定块大小  
            long chunkSize = 102400;
            //建立一个100K的缓冲区  
            byte[] buffer = new byte[chunkSize];
            //已读的字节数  
            long dataToRead = stream.Length;


            //添加Http头  
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition",
                string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));
            Response.AddHeader("Content-Length", dataToRead.ToString());


            while (dataToRead > 0 && Response.IsClientConnected)
            {
                int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
                Response.OutputStream.Write(buffer, 0, length);
                Response.Flush();
                Response.Clear();
                dataToRead -= length;
            }
            Response.Close();
        }
    }


    public void DownloadByBinary(string filePath, string fileName)
    {
        using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            //指定块大小  
            long chunkSize = 102400;
            //建立一个100K的缓冲区  
            byte[] bytes = new byte[chunkSize];
            //已读的字节数  
            long dataToRead = stream.Length;


            //添加Http头  
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition",
                string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));


            Response.AddHeader("Content-Length", bytes.Length.ToString());
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.Close();
        }
    }


    public void DownloadByBinaryBlock(string filePath, string fileName)
    {
        using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            //指定块大小  
            long chunkSize = 102400;
            //建立一个100K的缓冲区  
            byte[] buffer = new byte[chunkSize];
            //已读的字节数  
            long dataToRead = stream.Length;


            //添加Http头  
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition",
                string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(fileName)));
            Response.AddHeader("Content-Length", dataToRead.ToString());


            while (dataToRead > 0 && Response.IsClientConnected)
            {
                int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
                Response.BinaryWrite(buffer);
                Response.Flush();
                Response.Clear();


                dataToRead -= length;
            }
            Response.Close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值