上传或下载文件方法并写入日志

/// <summary>
        /// 下载并更新文件(本地客户端存在文件就用服务器文件覆盖掉,不存在该文件就创建并写入)
        /// </summary>
        /// <param name="serverFullName">服务器文件全名</param>
        /// <param name="localFullName">本地文件全名</param>
        /// <returns></returns>
        private bool DownUpdateFile(string serverFullName, string localFullName, string FolderName, out string mes)
        {
            try
            {
                int num = 0;
                num++;
                string url = txtdaml.Text.Trim();
                /// 创建WebClient实例
                WebClient myWebClient = new WebClient();
                //NetworkCredential nc = new NetworkCredential(ServerUserName, ServerPassWord);
                Uri addy = new Uri(serverFullName);
                myWebClient.Credentials = CredentialCache.DefaultCredentials; ;

                string[] arrayfile = Directory.GetFiles(txtdaml.Text);
                WebRequest webReq = WebRequest.Create(localFullName);
                WebResponse webRes = webReq.GetResponse();

                long fileLength = webRes.ContentLength;
                string fileName = Path.GetFileName(localFullName);
                //this.label4.Text = String.Format(
                //    CultureInfo.InvariantCulture,
                //    "上传进度 {0}/{1}",
                //    num,
                //    arrayfile.Length,
                //   );
                //rchtxtInfo.Text = rchtxtInfo.Text + string.Format("上传文件成功:{0},文件大小:{1}KB\n", fileName, (fileLength / 1024.0).ToString("N2")) + "\r\n";
                progressBar1.Maximum = (int)fileLength;
                progressBar1.Value = 0;
                Application.DoEvents();

                Stream stream = webRes.GetResponseStream();
                StreamReader sr = new StreamReader(stream);
                byte[] bufferbyte = new byte[fileLength];
                int allByte = (int)bufferbyte.Length;
                int startByte = 0;
                while (fileLength > 0)
                {
                    int downByte = stream.Read(bufferbyte, startByte, allByte);
                    if (downByte == 0)
                    {
                        break;
                    }
                    startByte = startByte + downByte;
                    allByte = allByte - downByte;

                    progressBar1.Value = startByte;
                    //即progressBar1.Value = progressBar1.Value + downByte;
                }

                //本地文件存在则覆盖,不存在则创建
                //string localFile = fbd.SelectedPath + "\\" + list[i];
                //CreateNewDiretory(localFullName);
                try
                {
                    myWebClient.UploadFile(addy, localFullName);
                    //rchtxtInfo.Text = rchtxtInfo.Text + string.Format("上传文件成功:{0}\n", FolderName + "\\" + fileName) + "\r\n";
                }
                catch
                {
                    //创建或更新覆盖文件
                    FileStream fs = new FileStream(serverFullName, FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(bufferbyte, 0, bufferbyte.Length);
                    stream.Close();
                    sr.Close();
                    fs.Close();
                    //rchtxtInfo.Text = rchtxtInfo.Text + string.Format("上传文件成功:{0}\n", FolderName + "\\" + fileName) + "\r\n";
                }
                Application.DoEvents();
                mes = "";
                return true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "上传中出现错误:");
                mes = ex.Message.ToString();
                return false;
            }
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值