private void worker_DoWork(object sender, DoWorkEventArgs e) { DownloadProduct downloadProduct = (DownloadProduct)e.Argument; var worker = sender as BackgroundWorker; worker.ReportProgress(0, String.Format("Processing Iteration 1")); DownloadServiceClient download = new DownloadServiceClient("NetTcpBinding_IDownloadService"); long DownloadSize; using (FileStream outputStream = new FileStream($@"{downloadProduct.path}.zip", FileMode.OpenOrCreate, FileAccess.Write)) { if (File.Exists($@"{downloadProduct.path}.zip")) { FileInfo file = new FileInfo($@"{downloadProduct.path}.zip"); DownloadSize = file.Length; outputStream.Position = DownloadSize; } else { DownloadSize = 0; } long FileSize = download.GetFileSize(downloadProduct.idGame); const int bufferSize = 16 * 1024; byte[] buffer = new byte[bufferSize]; using (Stream stream = download.DownloadProduct(downloadProduct.idGame, downloadProduct.idUser, downloadProduct.path, DownloadSize)) { int bytesRead = stream.Read(buffer, 0, bufferSize); while (bytesRead > 0) { FileInfo file = new FileInfo($@"{downloadProduct.path}.zip"); DownloadSize = file.Length; worker.ReportProgress(Convert.ToInt32((double)DownloadSize / (double)FileSize * (double)100), String.Format("Загружено {0} %", Convert.ToInt32((double)DownloadSize / (double)FileSize * (double)100))); outputStream.Write(buffer, 0, bytesRead); bytesRead = stream.Read(buffer, 0, bufferSize); outputStream.Flush(); } outputStream.Close(); worker.ReportProgress(100); } } ZipFile.ExtractToDirectory($@"{downloadProduct.path}.zip", downloadProduct.path); File.Delete($@"{downloadProduct.path}.zip"); download.Close(); }
private async void btn_download_Click(object sender, RoutedEventArgs e) { if (selectedFile != null) { /* * MessageBox.Show("Thông tin file đang được chọn để tải:\n " + * "\n- File Server IP: " + selectedFileServerIP + * "\n- File server Port: " + selectedFileServerPort + * "\n- File info:" + * selectedFile.ID.ToString() + ", " + * selectedFile.Name + ", " + * selectedFile.Size + ", " + * selectedFile.Path + ", " + * selectedFile.Type + "," + * selectedFile.MD5); */ dsc = new DownloadServiceClient(selectedFileServerIP, selectedFileServerPort); LabelDownloadStatus.Content = $"Downloading: {selectedFile.Name}"; byte[] receivedData = await dsc.BeginFileReceiver(selectedFile.Path, selectedFile.Size); LabelDownloadStatus.Content = "Computing hashsum"; MD5 md5 = MD5.Create(); string checksum = BitConverter.ToString(md5.ComputeHash(receivedData)).Replace("-", string.Empty); if (checksum.Equals(selectedFile.MD5)) { LabelDownloadStatus.Content = "Saving"; SaveFileDialog dialog = new SaveFileDialog(); if (dialog.ShowDialog() == true) { File.WriteAllBytes(dialog.FileName, receivedData); LabelDownloadStatus.Content = "File saved"; } else { LabelDownloadStatus.Content = "Download canceled"; } } else { LabelDownloadStatus.Content = "Checksum failed, please try again"; } } }