Ejemplo n.º 1
0
        private void _BGWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgWorker = sender as BackgroundWorker;

            _manager = new DownloadManager(bgWorker);

            // 如果下面的连接不可用,请自行更换一个可用的下载链接
            // 如果下载太快,请更换一个大点的文件进行测试
            //string url = "http://download.firefox.com.cn/releases-sha2/stub/official/zh-CN/Firefox-latest.exe";
            // string url = "http://down.360.cn/360sd/360sd_x64_std_5.0.0.7121A.exe";
            //string url = "http://localhost:8088/downloadHandler.ashx?fineName=D:\\PortableWCF\\UpLoadFile\\memex\\ccc\\Accessibility.dll";
            // string url = "http://localhost:8085/api/home/DownLoadBreak?fileName=D:\\a\\b\\Accessibility.dll";
            //string url = "http://localhost:8085/api/home/DownLoadBreak?fileName=D:\\a\\b\\setup.exe";
            //string url = "http://localhost:8085/api/home/DownLoadBreak?fileName=D:\\a\\b\\TOPK_TO.rar";
            //string url = "http://122.115.55.28:8085/api/home/DownLoadBreak?fileName=setup.exe";
            string url = "http://localhost:8085/api/home/DownLoadBreak?fileName=setup.exe";
            // string url = "http://localhost:8085/api/home/DownLoadBreak?fileName=D:\\a\\b\\setup.exe";
            Random rdm = new Random();
            string s   = rdm.Next().ToString();

            url += "?" + s;

            _manager.DownloadFile(url, "setup.exe");
        }
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);
        }