C# 断点下载

    [MessageContract]
    public class DownFileResult
    {
        //大小
        [MessageBodyMember]
        public long FileSize { get; set; }
        //状态
        [MessageHeader]
        public bool IsSuccess { get; set; }
        //文件数据
        [MessageBodyMember]
        public byte[] Buffer { get; set; }
      
    }


public static DownFileResult thisUploadData(string filePath, long length)
        {
            DownFileResult result = new DownFileResult();
            long currentPosition = length;

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] buffer = new byte[10240000];
                fs.Position = currentPosition;
                if (currentPosition + buffer.Length < fs.Length)
                {
                    int temp = fs.Read(buffer, 0, buffer.Length);
                    result.Buffer = buffer;
                    result.FileSize = fs.Length;
                    result.IsSuccess = true;
                    fs.Flush();
                    fs.Close();
                    return result;
                }
                else
                {
                    byte[] tempBuffer = new byte[fs.Length - currentPosition];
                    fs.Read(tempBuffer, 0, tempBuffer.Length);
                    result.Buffer = tempBuffer;
                    result.FileSize = fs.Length;
                    result.IsSuccess = true;
                    fs.Flush();
                    fs.Close();
                    return result;
                }
            }
        }
//以上服务端代码 下面是客户端接受
  /// <summary>
        /// 从服务端下载数据,并且保存到客户端本地
        /// </summary>
        /// <param name="construction_id"></param>
        public bool DownLoadData(string filePath)
        {
            bool rulst = false;
            using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
            {
                try
                {
                    long fileSize;
                    byte[] buffer;
                    string message;
                    do
                    {
                        //请求下载返回信息
                        gmapsiveClient.DownLoadGMapFile(fs.Length, "", out message, out buffer, out fileSize);
                        fs.Write(buffer, 0, buffer.Length);
                    }
                    while (fs.Length < fileSize);

                    rulst = true;
                }
                catch (Exception ex)
                {
                    rulst = false;
                }
                finally
                {
                    fs.Flush();
                    fs.Close();

                }
                return rulst;
            }
        }

        #endregion

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值