断点续传

 /// <summary>
         /// 服务端: 
         /// </summary>
         /// <param name="FileName">更新文件包名</param>
         /// <param name="Offset">偏移</param>
         /// <param name="Count">每次读取字节数 单位KB</param>
         /// <returns>字节组</returns>
         [WebMethod(Description = "<b>大文件下载</b> 测试")]
         public byte[] getFile(string FileName, long Offset, int Count)
         {
             //指定下载文件夹+文件名
            string strPath = @"E:\" + FileName;

            if (Offset < 0) { Offset = 0; }

            byte[] btBuf = new byte[Count];

            if (System.IO.File.Exists(strPath))
             {
                 System.IO.FileStream fs = new System.IO.FileStream(strPath, System.IO.FileMode.Open); 
                 if (Offset < fs.Length)
                 {
                     if (0 < (int)(fs.Length - Offset) && (int)(fs.Length - Offset) < Count)
                     {
                         Count = (int)(fs.Length - Offset);
                     }
                     btBuf=new byte[Count];

                    fs.Seek(Offset, System.IO.SeekOrigin.Begin);
                     fs.Read(btBuf, 0, Count);
                 }
                 else
                 { btBuf = null; }
                 fs.Flush();
                 fs.Close();
                 fs.Dispose();                
             }
             return btBuf;
         }

//--------------------------------客户端调用:

 

 private string DownLoadPath(string strClientFilePath, string fileName)
     {
         string result = "DownLoading...";

        ws1.Service s1 = new ws1.Service();  

        //本地数据
        System.IO.FileStream ns;

        //文件保存路径
        string strPath = strClientFilePath + @"" + fileName;

        //源文件名
        string serverFileName = @"Enterprise.msi";

        //传输数据长度,每次取字节组大小
        int count = 1024 * 100;
         
         //申请内存,存放取回的数据
        byte[] buffer = new byte[count];

        //文件偏移
        long offset = 0;

        //取回数据字节长度
        int bufferSize = 1;

        //-------------
         while (bufferSize > 0)
         {
             
             //读取本地文件信息
            ns = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
             
             //获取本地数据长度,设定从服务器取数据偏移指针位置
            offset = ns.Length;

            //取服务端数据
            buffer = s1.getFile(serverFileName, offset, count);
             if (buffer!=null)
             {
                 ns.Seek(offset, System.IO.SeekOrigin.Begin);
                 count = buffer.Length;
                 ns.Write(buffer, 0, count);
             }
             else
             {
                 bufferSize = -1;
                 result = "Successful !";
             }
             ns.Flush();
             ns.Close();
             ns.Dispose();
         }
         return result;        
     }
View Code
原文地址:https://www.cnblogs.com/feige/p/6017365.html