WebClient实现远程下载的相关方法

2017-05-03 10:26:57  访问(5311) 赞(0) 踩(0)


        /// <summary>
        /// 
        /// </summary>
        AutoUpdateDataInfo di = null;

        /// <summary>
        /// 下载更新程序
        /// </summary>
        /// <param name="_di"></param>
        public UpdateDownForm(AutoUpdateDataInfo _di)
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;

            di = _di;

            button_下一步.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateDownForm_Load(object sender, EventArgs e)
        {
            if (di != null)
            {
                textBox_下载地址.Text = di.DownLoadUrl;
                textBox_保存文件.Text = di.DownLoadFileName;


                OKClick();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        WebClient webClientV = null;

        /// <summary>
        /// 
        /// </summary>
        bool isDownLoading = false;

        /// <summary>
        /// OK点击事件
        /// </summary>
        public void OKClick()
        {
            if (isDownLoading)
                return;

            string downUrl = textBox_下载地址.Text.Trim();

            if (downUrl == null || downUrl.Length == 0)
            {

                MessageBox.Show("请选择下载地址");
                textBox_下载地址.Focus();
                textBox_下载地址.Select();
                return;

            }


            string saveFile = textBox_保存文件.Text.Trim();

            if (saveFile == null || saveFile.Length == 0)
            {

                MessageBox.Show("请选择保存文件");
                textBox_保存文件.Focus();
                textBox_保存文件.Select();
                return;

            }

            DeleteOutputFile(saveFile);

            if (webClientV == null)
                webClientV = new WebClient();

            if (webClientV.IsBusy)//是否存在正在进行中的Web请求
            {
                webClientV.CancelAsync();
            }


            //为webClient添加事件
            webClientV.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
            webClientV.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
            //开始下载
            webClientV.DownloadFileAsync
                (
                    new Uri(downUrl),
                    saveFile
                );

            isDownLoading = true;
            button_取消.Enabled = true;
        }


        /// <summary>
        /// 2、删除旧的发布文件
        /// </summary>
        /// <param name="fileName"></param>
        protected void DeleteOutputFile(string fileName)
        {
            if (fileName == null || fileName.Length == 0
                  || fileName.Trim().Length == 0)
            {

                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "传入的字符串参数:"
                        + "string fileName"
                        + "为null或trim后为空。"
                    );
            }

            FileInfo info = new FileInfo(fileName);

            if (info.Exists)
            {
                info.Delete();
                return;
            }

            // 如果文件目录不存在 //
            if (!info.Directory.Exists)
                info.Directory.Create();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
            this.lbl_pro.Text = e.ProgressPercentage.ToString() + "%";
            this.lbl_detail.Text = string.Format("正在下载文件,完成进度{0}/{1}(字节)"
                                , e.BytesReceived
                                , e.TotalBytesToReceive);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                this.Close();
                DialogResult = DialogResult.Cancel;
            }
            else
            {
                // MessageBox.Show("下载完成!");
                //this.Close();
                //DialogResult = DialogResult.OK;

                button_下一步.Enabled = true;
            }

            isDownLoading = false;
            webClientV = null;
            button_取消.Enabled = false;
        }


        /// <summary>
        /// 取消点击事件
        /// </summary>
        public void CancelClick()
        {
            if (webClientV != null)
            {
                this.webClientV.CancelAsync();

                if (webClientV != null)
                {
                    this.webClientV.Dispose();
                }

                this.webClientV = null;
                isDownLoading = false;
                button_取消.Enabled = false;
            }
        }

        /// <summary>
        /// 下载取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_取消_Click(object sender, EventArgs e)
        {
            try
            {
                CancelClick();
            }
            catch (Exception err)
            {
                MsgForm.MsgException(err);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateDownForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                CancelClick();
            }
            catch (Exception err)
            {
                MsgForm.MsgException(err);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_下一步_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
                DialogResult = DialogResult.OK;
            }
            catch (Exception err)
            {
                MsgForm.MsgException(err);
            }
        }


标签:WebClient实现远程下载的相关方法 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)