Ejemplo n.º 1
0
        private void _BGWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            DownloadInfo info = e.UserState as DownloadInfo;

            if (info != null)
            {
                this.progressBar1.Value = info.Persent;
                _log.Log("Persent is " + info.Persent + "% .");

                if (info.Message == "已取消下载")
                {
                    this.Close();
                }
                if (info.Message == "下载失败")
                {
                    this.label1.Text = "下载失败";
                }
            }
        }
Ejemplo n.º 2
0
        private bool WebClientDownloadInstallerFile(string url, string fileName)
        {
            bool            resumeDownload         = IsResume(url, fileName);
            string          tempFileName           = fileName + ".temp";
            string          tempInfoFileName       = fileName + ".temp" + ".info";
            bool            isDownloadSuccessfully = false;
            FileMode        fm         = FileMode.Create;
            Stream          stream     = null;
            FileStream      fileStream = null;
            HttpWebResponse response   = null;

            this._downloadStopWatch.Start();
            try
            {
                Uri            installerUrl   = new Uri(url);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
                if (resumeDownload)
                {
                    FileInfo fn = new FileInfo(tempFileName);
                    httpWebRequest.AddRange(fn.Length);
                    fm = FileMode.Append;
                }

                response = (HttpWebResponse)httpWebRequest.GetResponse();
                stream   = response.GetResponseStream();
                var etag = GetEtag(response);
                if (File.Exists(tempInfoFileName) && !string.IsNullOrEmpty(etag))
                {
                    FileStream   f  = new FileStream(tempInfoFileName, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(f);
                    sw.WriteLine(etag);
                    sw.Close();
                    f.Close();
                }

                double contentLength    = DownloadManager.GetContentLength(response);
                byte[] buffer           = new byte[BufferSize];
                long   downloadedLength = 0;
                int    currentDataLength;
                fileStream = new FileStream(tempFileName, fm);

                while ((currentDataLength = stream.Read(buffer, 0, BufferSize)) > 0 && !this._cancelDownload)
                {
                    fileStream.Write(buffer, 0, currentDataLength);
                    downloadedLength += (long)currentDataLength;

                    if (this._downloadStopWatch.ElapsedMilliseconds > 1000)
                    {
                        this._downloadStopWatch.Reset();
                        this._downloadStopWatch.Start();

                        double doubleDownloadPersent = 0.0;
                        if (contentLength > 0.0)
                        {
                            doubleDownloadPersent = (double)downloadedLength / contentLength;
                            if (doubleDownloadPersent > 1.0)
                            {
                                doubleDownloadPersent = 1.0;
                            }
                        }

                        int          intDownloadPersent = (int)(doubleDownloadPersent * 100);
                        DownloadInfo info = new DownloadInfo()
                        {
                            Message = "xxx", Persent = intDownloadPersent
                        };
                        DownloadingStatusChanged(info);
                    }
                }

                if (this._cancelDownload)
                {
                    DownloadInfo info = new DownloadInfo()
                    {
                        Message = "已取消下载", Persent = 100
                    };
                    DownloadingStatusChanged(info);
                }
                else if (currentDataLength >= 0)
                {
                    // downlown correct
                    isDownloadSuccessfully = true;
                }
            }
            catch (Exception ex)
            {
                // todo
            }
            finally
            {
                this._downloadStopWatch.Stop();
                if (fileStream != null)
                {
                    fileStream.Flush();
                    fileStream.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }

                if (response != null)
                {
                    response.Close();
                }
            }
            if (isDownloadSuccessfully)
            {
                if (File.Exists(fileName))
                {
                    Util.DeleteFileIfExists(fileName);
                }
                DownloadInfo info = new DownloadInfo()
                {
                    Message = "xxx", Persent = 100
                };
                DownloadingStatusChanged(info);
                File.Move(tempFileName, fileName);

                string tempFileInfoName = fileName + ".temp.info";
                if (File.Exists(tempFileInfoName))
                {
                    Util.DeleteFileIfExists(tempFileInfoName);
                }
            }
            return(isDownloadSuccessfully);
        }