• C#断点续传的实现


    断点续传的实现方式有很多,下面介绍个依赖本地以下载的文件大小来实现断点续传

            public static void HttpDownloadEx(string url,
                string path,
                bool overwrite,
                Action<string, HttpWebResponse> doneCallback = null,
                Action<string, string, long, long> downloadingCallback = null) 
            {
    
                // 设置参数
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                //发送请求并获取相应回应数据
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    
                string contentType = response.Headers["Content-Type"];
    
                //断点续传
                FileStream fStream = null;
                long sPosition = 0;
                Stream responseStream = null;
    
                try
                {
                    long totalLength = response.ContentLength;
                    if (System.IO.File.Exists(path))
                    {
                        fStream = System.IO.File.OpenWrite(path);
                        sPosition = fStream.Length;
                        if (sPosition == totalLength)
                        {
                            doneCallback?.Invoke(path, response); //文件是完整的,直接结束下载任务
                            return;
                        }
                        fStream.Seek(sPosition, SeekOrigin.Current);
                    }
                    else
                    {
                        fStream = new FileStream(path, FileMode.Create);
                        sPosition = 0;
                    }
    
                    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                    if (sPosition > 0)
                    {
                        myRequest.AddRange(sPosition);             //设置Range值
                    }
    
                    //向服务器请求,获得服务器的回应数据流
                    responseStream = myRequest.GetResponse().GetResponseStream();
    
                    //定义一个字节数据
                    byte[] btContent = new byte[512];
                    int intSize = 0;
                    intSize = responseStream.Read(btContent, 0, 512);
                    while (intSize > 0)
                    {
                        fStream.Write(btContent, 0, intSize);
                        intSize = responseStream.Read(btContent, 0, 512);
                        downloadingCallback?.Invoke(path, contentType, totalLength, fStream.Length);
                    }
                }
                catch
                {//(Exception ex)
                    throw;
                }
                finally
                {
                    //关闭流
                    if (responseStream != null)
                    {
                        responseStream.Close();
                        responseStream.Dispose();
                    }
                    if (fStream != null)
                    {
                        fStream.Close();
                        fStream.Dispose();
                    }
                }
            }
  • 相关阅读:
    Asp.Net中Word,Excel等office com组件操作权限的问题 ————转自rainpig2008
    正则表达式摘录
    html5 Canvas画图2:画线条
    彻底理解函数声明与函数表达式优先级问题
    javascript定义变量和优先级的问题
    html5 Canvas画图4:填充和渐变
    讨论下canvas画椭圆的方法
    html5 Canvas画图3:1像素线条模糊问题
    [推荐]实在受不了canvas的语法了!我要让他支持链式语法!
    html5 Canvas画图5:画曲线之arc
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/14044208.html
Copyright © 2020-2023  润新知