以前公司有个客户端当服务器更新版本的时候客户端也需要更新,可是人家客户网速不给了哦!老是下载更新文件下了一部分网速不行就失败了,同事哥哥说要用断点续传哦神马东东哦!

解决方案 »

  1.   

    服务端程序/// <summary>
            /// 客户端升级前先获取服务器版本号
            /// </summary>
            /// <returns></returns>
            [WebMethod(Description = "取得更新版本")]        public string GetVer()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("update.xml"));
                XmlElement root = doc.DocumentElement;
                return root.SelectSingleNode("version").InnerText;
            }
            
            
            /// <summary>
            /// 获取更新软件路径及详细信息
            /// </summary>
            /// <returns></returns>
            [WebMethod(Description = "在线更新软件")]
            public string GetUpdateData()
            {
                //取得更新的xml模板内容
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("update.xml"));
                XmlElement root = doc.DocumentElement;
                
                //看看有几个文件需要更新
                XmlNode updateNode = root.SelectSingleNode("filelist");
                string path = updateNode.Attributes["sourcepath"].Value;
                int count = int.Parse(updateNode.Attributes["count"].Value);
                
                //将xml中的value用实际内容替换
                for (int i = 0; i < count; i++)
                {
                    XmlNode itemNode = updateNode.ChildNodes[i];
                    string fileName = path + itemNode.Attributes["name"].Value;
                    FileStream fs = File.OpenRead(Server.MapPath(fileName));
                    itemNode.Attributes["size"].Value = fs.Length.ToString();                itemNode.Attributes["prefect"].Value = "0";
                    //为V2.4.3升级准备
                    BinaryReader br = new BinaryReader(fs);                //这里是文件的实际内容,使用了Base64String编码
                    //itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length), 0, (int)fs.Length);
                    br.Close();
                    fs.Close();
                }
                return doc.InnerXml;
            }        #region 获取文件列表以及信息列表
            [WebMethod(Description = "获取文件概要信息")]
            public UpdateInfoTable[] GetUpdateInfo()
            {
                
                UpdateInfoTable[] tables = null;//返回类数组
                try
                {
                    //  List<UpdateInfoTable> list = new List<UpdateInfoTable>();
                    Dictionary<string, string> dr = new Dictionary<string, string>();
                                   //取得更新的xml模板内容
                    XmlDocument doc = new XmlDocument();
                    doc.Load(Server.MapPath("update.xml"));
                    XmlElement root = doc.DocumentElement;                //看看有几个文件需要更新
                    XmlNode updateNode = root.SelectSingleNode("filelist");
                    string path = updateNode.Attributes["sourcepath"].Value;
                    int count = int.Parse(updateNode.Attributes["count"].Value);
                    tables = new UpdateInfoTable[count];
                    for (int i = 0; i < count; i++)
                    {
                        tables[i] = new UpdateInfoTable();
                        XmlNode itemNode = updateNode.ChildNodes[i];
                        string fileName = path + itemNode.Attributes["name"].Value;
                        string name = itemNode.Attributes["name"].Value;//文件名称                    FileStream fs = File.OpenRead(Server.MapPath(fileName));
                        itemNode.Attributes["size"].Value = fs.Length.ToString();
                        string fileLength = fs.Length.ToString();//文件大小
                        if (!dr.ContainsValue(name))
                        {
                            dr.Add(name, fileLength);                        tables[i].FileLength = fileLength;
                            tables[i].FileName = name;                    }
                    }
                }
                catch
                {
                    tables = null;
                }
                return tables ;        }
            #endregion        #region 由文件名称和数据帧断点续传
            [WebMethod(Description = "取任需上传数据")]
            public Byte[] GetContinuePacketRequest(string fileName, int nFrame)
            {
                byte[] byReturn = null;
                try
                {
                    //取得更新的xml模板内容
                    string path = "./update/" + fileName;
                    string filePathres = string.Format(@"{0}", path);//路径
                    string fileFullName = HttpContext.Current.Request.PhysicalApplicationPath + filePathres.Replace("/", "\\");                //打开文件流
                    FileStream fs = File.OpenRead(fileFullName);                //游标记录从哪里开始取数据
                    long offset = nFrame * BUFFER_SIZE;
                    fs.Seek(offset, SeekOrigin.Begin);                BinaryReader br = new BinaryReader(fs);
                    if (BUFFER_SIZE * (nFrame + 1) > fs.Length)
                    {
                        int size = Convert.ToInt32(fs.Length - BUFFER_SIZE * nFrame);
                        byReturn = br.ReadBytes(size);
                    }
                    else
                    {
                        byReturn = br.ReadBytes(BUFFER_SIZE);
                    }
                    br.Close();
                    fs.Close();
                
                }
                catch
                {
                }
                return byReturn;
                 
            }        #endregion
      

  2.   

    客户端:
        /// <summary>
            /// 执行下载
            /// </summary>
            public static bool ExecutePro()
            {
                 byte[] getBytes = null;
                 Services ser = new Services();
                 bool fileExist = false;
                 string downFile = "";               System.Xml.XmlDocument doc =new XmlDocument ();
                try
                {
                    #region 判断下载文件文件是否已经存在如果存在不需下载
                    string str = Application.StartupPath + @"\update.xml";
                    bool s = System.IO.File.Exists(str);
                    if (!s)
                    {
                        doc = ((System.Xml.XmlDocument)ser.GetNewVersionContent());
                        if (doc == null)
                        {
                            MessageBox.Show("未能从服务器上获取更新版本信息,请联系网站管理员!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            return false;
                        }
                        doc.Save(Application.StartupPath + @"\update.xml");
                    }
                    else
                    {
                        doc.Load(Application.StartupPath + @"\update.xml");
                    }
                    dp = new DownLoadPacket();
                    dp.Buffer = GetDownLoadLength().ToString();
                    #endregion                #region 查看download文件夹是否存在,不存在则新建
                    downFile = Application.StartupPath + @"\download\";
                    fileExist = Directory.Exists(downFile);                if (!fileExist)   
                    {
                        Directory.CreateDirectory(downFile); //新建文件夹   
                    }                string imageFile = Application.StartupPath + @"\images\";
                    bool imageExist = Directory.Exists(imageFile);
                    if (!imageExist)
                    {
                        Directory.CreateDirectory(imageFile); //新建文件夹   
                    }
                    #endregion                #region 处理自动升级程序自动更新, 断点续传
                   
                    XmlElement root = doc.DocumentElement;
                    XmlNode updateNode = root.SelectSingleNode("filelist");
                    int count = int.Parse(updateNode.Attributes["count"].Value);
                    for (int i = 0; i < count; i++)
                    {
                        int nFrame = 0;
                        XmlNode itemNode = updateNode.ChildNodes[i];
                        string fileName =  itemNode.Attributes["name"].Value;//文件名称
                        string size = itemNode.Attributes["size"].Value;//文件大小
                        string prefect = itemNode.Attributes["prefect"].Value;//文件的完成度                    #region//如果文件下载度与文件大小不一致说明文件未下载完成
                        if (size !=prefect)
                        {
                            #region //如果执行文件为空说明未曾下载
                            if (prefect =="0")
                            {
                                double nMaxFrame = Math.Ceiling(Convert.ToDouble(size) / BUFFER_SIZE);
                                int nmaxFrame = Convert.ToInt32(nMaxFrame);
                                nFrame = 0;
                                for (int j = 0; j < nmaxFrame ; j++)
                                {
                                  
                                    //从服务器取字节数字
                                    getBytes = ser.GetContinuePacket(fileName, nFrame);                                //将下载的文件放在download文件夹下
                                    string fileFullName = Application.StartupPath + @"\download\" + fileName;//文件全路径
                                    FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate);
                                    long offset = nFrame * BUFFER_SIZE;
                                    fs.Seek(offset, SeekOrigin.Begin);
                                    BinaryWriter bw = new BinaryWriter(fs);
                                    int updatesize = 0;
                                    if (BUFFER_SIZE * (nFrame + 1) > Convert.ToInt32(size))
                                    {
                                        updatesize = Convert.ToInt32(Convert.ToInt32(size) - BUFFER_SIZE * nFrame);
                                        bw.Write(getBytes);                                }
                                    else
                                    {
                                        updatesize = BUFFER_SIZE;
                                        bw.Write(getBytes);
                                    }
                                    bw.Close();
                                    fs.Close();
                                    //更新进度条的状态
                                    //if (nFrame >= 1)
                                    //{
                                    dp.Buffer = (Convert.ToInt32(dp.Buffer) + updatesize).ToString();
                                     Updateprefect(fileName, updatesize.ToString());
                                    //}
                                    
                                    nFrame++;
                                    continue;
                                }                        }
                            #endregion                        #region 如果文件部分下载
                            else
                            {
                                double nMaxFrame = Math.Ceiling(Convert.ToDouble(size) / BUFFER_SIZE);
                                //看已经下载了有几帧
                                nFrame = Convert.ToInt32(prefect) / BUFFER_SIZE;//开始帧
                                for (int j = nFrame; j < Convert.ToInt32(nMaxFrame); j++)
                                {
                                    //从服务器取字节数字
                                    getBytes = ser.GetContinuePacket(fileName, nFrame);                                //将下载的文件放在download文件夹下
                                    string fileFullName = Application.StartupPath + @"\download\" + fileName;//文件全路径
                                    FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate);
                                    long offset = nFrame * BUFFER_SIZE;
                                    fs.Seek(offset, SeekOrigin.Begin);
                                    BinaryWriter bw = new BinaryWriter(fs);                                int updatesize = 0;
                                    if (BUFFER_SIZE * (nFrame + 1) > Convert.ToInt32(size))
                                    {
                                        updatesize = Convert.ToInt32(Convert.ToInt32(size) - BUFFER_SIZE * nFrame);
                                        bw.Write(getBytes);                                }
                                    else
                                    {
                                        updatesize = BUFFER_SIZE;
                                        bw.Write(getBytes);
                                    }
                                   
                                    bw.Close();
                                    fs.Close();
                                    //更新进度条的状态
                                    dp.Buffer = (Convert.ToInt32(dp.Buffer) + updatesize).ToString();
                                    Updateprefect(fileName, updatesize.ToString());
                                    nFrame++;
                                     
                                }
     
                            }
                            #endregion                    }
                        #endregion                    #region//文件已经下载完成
                        else
                        {
                            continue;
                        }
                        #endregion                }
                    #endregion                return true;
                }
                catch(Exception ex)
                {
                    return false;
                }
            }
      

  3.   

    http://www.google.com.hk/#newwindow=1&q=c%23+%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0&safe=strict