用流的方式向客户端输出文件

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;
using Ths.Data;

 

public void DownFile(string filepath, string filename)
        {
            try
            {
                HttpContext.Current.Response.Clear();
                if (!System.IO.File.Exists(filepath))
                {
                    throw new Exception("文件(" + filepath + ")不存在!");
                }
                FileStream stream1 = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader reader1 = new BinaryReader(stream1);
                try
                {
                    try
                    {
                        HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
                        HttpContext.Current.Response.Buffer = false;
                        long num1 = stream1.Length;
                        long num2 = 0;
                        int num3 = 0x2800;
                        double num4 = (0x3e8 * num3) / 0xfa000;
                        int num5 = ((int)Math.Floor(num4)) + 1;
                        if (HttpContext.Current.Request.Headers["Range"] != null)
                        {
                            HttpContext.Current.Response.StatusCode = 0xce;
                            string[] textArray1 = HttpContext.Current.Request.Headers["Range"].Split(new char[] { '=', '-' });
                            num2 = Convert.ToInt64(textArray1[1]);
                        }
                        long num9 = num1 - num2;
                        HttpContext.Current.Response.AddHeader("Content-Length", num9.ToString());
                        if (num2 != 0)
                        {
                            HttpContext.Current.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", num2, num1 - 1, num1));
                        }
                        HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
                        HttpContext.Current.Response.ContentType = "application/octet-stream";
                        string text1 = Path.GetFileName(filepath);
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8));
                        reader1.BaseStream.Seek(num2, SeekOrigin.Begin);
                        double num6 = (num1 - num2) / ((long)num3);
                        int num7 = ((int)Math.Floor(num6)) + 1;
                        for (int num8 = 0; num8 < num7; num8++)
                        {
                            if (HttpContext.Current.Response.IsClientConnected)
                            {
                                HttpContext.Current.Response.BinaryWrite(reader1.ReadBytes(num3));
                            }
                            else
                            {
                                num8 = num7;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                }
                finally
                {
                    reader1.Close();
                    stream1.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            HttpContext.Current.Response.End();
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值