zoukankan      html  css  js  c++  java
  • 断点续传

     /// <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
  • 相关阅读:
    下一个ajax异步请求被挂起问题
    借鉴别人的Oracle 11g安装和卸载图文教程
    Html5 实现网页截屏 页面生成图片(图文)
    Oracle修改字段类型方法小技巧
    基于轻量级ORM框架Dapper的扩展说明
    JavaScript+html5 canvas实现本地截图教程
    SkipList跳表基本原理
    Oracle日期查询:季度、月份、星期等时间信息
    设计模式之模板模式
    设计模式之解释器模式
  • 原文地址:https://www.cnblogs.com/feige/p/6017365.html
Copyright © 2011-2022 走看看