文件下载

//引用命名空间
using System.IO;
using System.Text;
using System.Web;

// 文件下载
public static bool DownloadFile(string path)
        {
            HttpRequest request = HttpContext.Current.Request;
            HttpResponse response = HttpContext.Current.Response;
            try
            {
                FileInfo info = new FileInfo(HttpContext.Current.Server.MapPath(path));
                if (info.Exists)
                {
                    string name = info.Name;
                    string extension = info.Extension;
                    name = name.Substring(0, name.Length - extension.Length);
                    while (HttpUtility.UrlEncode(name + extension, Encoding.UTF8).Length > 0x9c)
                    {
                        name = name.Substring(0, name.Length - 1);
                    }
                    name = HttpUtility.UrlEncode(name + extension, Encoding.UTF8);
                    response.Clear();
                    if (info.Length < 0xfa000L)
                    {
                        response.ContentType = "application/octet-stream";
                        response.AddHeader("Content-Disposition", "attachment;filename=" + name);
                        response.WriteFile(info.FullName);
                    }
                    else
                    {
                        FileStream input = new FileStream(HttpContext.Current.Server.MapPath(path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        BinaryReader reader = new BinaryReader(input);
                        try
                        {
                            long length = input.Length;
                            long num2 = 0L;
                            int count = 0x2800;
                            if (request.Headers["Range"] != null)
                            {
                                response.StatusCode = 0xce;
                                num2 = Convert.ToInt64(request.Headers["Range"].Split(new char[] { '=', '-' })[1]);
                            }
                            response.AddHeader("Content-Length", (length - num2).ToString());
                            if (num2 != 0L)
                            {
                                response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", num2, length - 1L, length));
                            }
                            response.ContentType = "application/octet-stream";
                            response.AddHeader("Content-Disposition", "attachment;filename=" + name);
                            reader.BaseStream.Seek(num2, SeekOrigin.Begin);
                            int num4 = ((int)Math.Floor((double)(((double)(length - num2)) / ((double)count)))) + 1;
                            for (int i = 0; i < num4; i++)
                            {
                                if (response.IsClientConnected)
                                {
                                    response.BinaryWrite(reader.ReadBytes(count));
                                    response.Flush();
                                }
                                else
                                {
                                    i = num4;
                                }
                            }
                        }
                        catch
                        {
                            return false;
                        }
                        finally
                        {
                            reader.Close();
                            input.Close();
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值