protected virtual Task GetDownloadFileTask(string sourceFile, string tmpFile, CancellationToken ct) { var downloadFileTask = new Task(() => { if (!File.Exists(tmpFile)) { var service = ServiceProviderProxy.GetInstallerWebService(); RaiseOnProgressChangedEvent(0); RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage); Log.WriteStart("Downloading file"); Log.WriteInfo(string.Format("Downloading file \"{0}\" to \"{1}\"", sourceFile, tmpFile)); long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { // Throw OperationCancelledException if there is an incoming cancel request ct.ThrowIfCancellationRequested(); content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(tmpFile, content); downloaded += content.Length; // Update download progress RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, string.Format(DownloadProgressMessage, downloaded / 1024, fileSize / 1024)); RaiseOnProgressChangedEvent(Convert.ToInt32((downloaded * 100) / fileSize)); if (content.Length < ChunkSize) { break; } } RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, "100%"); Log.WriteEnd(string.Format("Downloaded {0} bytes", downloaded)); } }, ct); return(downloadFileTask); }
private void DownloadFile(string sourceFile, string destinationFile) { try { var service = ServiceProviderProxy.GetInstallerWebService(); // Log.WriteStart("Downloading file"); Log.WriteInfo(string.Format("Downloading file \"{0}\" to \"{1}\"", sourceFile, destinationFile)); long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(destinationFile, content); downloaded += content.Length; //update progress bar RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, string.Format(DownloadProgressMessage, downloaded / 1024, fileSize / 1024)); // RaiseOnProgressChangedEvent(Convert.ToInt32((downloaded * 100) / fileSize)); if (content.Length < ChunkSize) { break; } } // RaiseOnStatusChangedEvent(DownloadingSetupFilesMessage, "100%"); // Log.WriteEnd(string.Format("Downloaded {0} bytes", downloaded)); } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } throw; } }
private void DownloadFile(string sourceFile, string destinationFile, ProgressBar progressBar) { try { Log.WriteStart("Downloading file"); Log.WriteInfo(string.Format("Downloading file \"{0}\" to \"{1}\"", sourceFile, destinationFile)); lblValue.Text = string.Empty; long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(destinationFile, content); downloaded += content.Length; //update progress bar lblValue.Text = string.Format("{0} KB of {1} KB", downloaded / 1024, fileSize / 1024); progressBar.Value = Convert.ToInt32((downloaded * 100) / fileSize); if (content.Length < ChunkSize) { break; } } lblValue.Text = string.Empty; Log.WriteEnd(string.Format("Downloaded {0} bytes", downloaded)); } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } throw; } }
private void DownloadFile(string sourceFile, string destinationFile, ProgressBar progressBar) { try { long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(destinationFile, content); downloaded += content.Length; //update progress bar progressBar.Value = Convert.ToInt32((downloaded * 100) / fileSize); if (content.Length < ChunkSize) { break; } } } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } throw; } }