Beispiel #1
0
        /// <summary>
        /// 开始下载
        /// </summary>
        public void StartDownload(string mainWin = "MainDialog")
        {
            IsDownloading = true;
            DownloadProgressDialogVM downloadProgressDialogVM = new DownloadProgressDialogVM("");
            //显示 等待窗
            var view = new DownloadProgressDialog
            {
                DataContext = downloadProgressDialogVM
            };
            //1.等待窗 (2.执行下载, 3.关闭 等待)
            var result = DialogHostEx.ShowDialog(GlobalUser.MainWin, view, ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
            //view.DownloadingSpeedText.Text = "正在下载试题,已完成 100%";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.DownloadUrl);

            if (DownloadBytes > 0)
            {
                request.AddRange(DownloadBytes);
            }
            request.BeginGetResponse(ar =>
            {
                var response = request.EndGetResponse(ar);
                if (this.TotalBytes == 0)
                {
                    this.TotalBytes = response.ContentLength;
                }
                using (var writer = new FileStream(this.FileName, FileMode.OpenOrCreate))
                {
                    using (var stream = response.GetResponseStream())
                    {
                        while (IsDownloading)
                        {
                            byte[] data    = new byte[readBytes];
                            int readNumber = stream.Read(data, 0, data.Length);
                            if (readNumber > 0)
                            {
                                writer.Write(data, 0, readNumber);
                                DownloadBytes += readNumber;
                            }
                            int downloadingSpeed = Convert.ToInt32((DownloadBytes * 100 / TotalBytes));
                            downloadProgressDialogVM.DownloadingSpeedText       = $"正在下载试题,已完成 {downloadingSpeed}%";
                            downloadProgressDialogVM.DownloadingSpeed           = downloadingSpeed == 100.00d ? 99.99d : downloadingSpeed;
                            downloadProgressDialogVM.DownloadingSpeedVisibility = downloadingSpeed > 1.00f ? Visibility.Visible : Visibility.Hidden;
                            if (DownloadBytes == TotalBytes)
                            {
                                break;
                            }
                        }
                    }
                }
                Complete();
            }, null);
        }
Beispiel #2
0
        /// <summary>
        /// 开始下载
        /// </summary>
        public void StartDownload(Dictionary <string, string> _aduioFileDic, string savePath,
                                  string mainWin = "MainDialog")
        {
            int d_count = 0;

            string d_url = ""; //下载 路径
            string s_url = ""; //保存路径

            IsDownloading = true;
            DownloadProgressDialogVM downloadProgressDialogVM = new DownloadProgressDialogVM("");
            //显示 等待窗
            var view = new DownloadProgressDialog
            {
                DataContext = downloadProgressDialogVM
            };

            //1.等待窗 (2.执行下载, 3.关闭 等待)
            var result = DialogHostEx.ShowDialog(GlobalUser.MainWin, view, ExtendedOpenedEventHandler,
                                                 ExtendedClosingEventHandler);

            //view.DownloadingSpeedText.Text = "正在下载试题,已完成 100%";

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                for (int i = 0; i < _aduioFileDic.Keys.Count; i++)
                {
                    d_url = $"{WebApiProxy.MEDIAURL}{_aduioFileDic[_aduioFileDic.Keys.ToList()[i]]}";
                    s_url = Path.Combine(savePath,
                                         SecurityHelper.HmacMd5Encrypt(Path.GetFileNameWithoutExtension(d_url), GlobalUser.FILEPWD,
                                                                       Encoding.UTF8)
                                         .ToLower() + ".qf");

                    downloadProgressDialogVM.DownloadingCount =
                        Convert.ToInt32(((i + 1) / _aduioFileDic.Keys.Count) * 100);
                    downloadProgressDialogVM.DownloadingCountText = $"总进度 {(i + 1)}/{_aduioFileDic.Keys.Count}";

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(d_url);
                    //if (DownloadBytes > 0)
                    //{
                    //    request.AddRange(DownloadBytes);
                    //}

                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    //var response = request.EndGetResponse(ar);
                    DownloadBytes = TotalBytes = 0;
                    if (this.TotalBytes == 0)
                    {
                        this.TotalBytes = response.ContentLength;
                    }
                    using (var writer = new MemoryStream())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            while (DownloadBytes != TotalBytes)
                            {
                                byte[] data    = new byte[readBytes];
                                int readNumber = stream.Read(data, 0, data.Length);
                                if (readNumber > 0)
                                {
                                    writer.Write(data, 0, readNumber);
                                    DownloadBytes += readNumber;
                                }

                                int downloadingSpeed = Convert.ToInt32((DownloadBytes * 100 / TotalBytes));
                                downloadProgressDialogVM.DownloadingSpeedText = $"当前 {downloadingSpeed}%";
                                downloadProgressDialogVM.DownloadingSpeed     =
                                    downloadingSpeed == 100.00d ? 99.99d : downloadingSpeed;
                                downloadProgressDialogVM.DownloadingSpeedVisibility =
                                    downloadingSpeed > 1.00f ? Visibility.Visible : Visibility.Hidden;
                            }
                        }

                        if (DownloadBytes == TotalBytes)
                        {
                            // Set the position to the beginning of the stream.
                            writer.Seek(0, SeekOrigin.Begin);

                            FileSecretHelper.EncryptFileBybt(writer, s_url);
                            d_count++;

                            if (d_count == _aduioFileDic.Keys.Count)
                            {
                                Complete();
                            }
                        }
                    }
                }
            }));
        }