//-------------------------------------------------------------------------------- //сохраняет файл public void DownloadFile(StreamWithProgress fileSourceStream, MyFile file ) { logger.Debug("Получение файла с сервера " + file.ToString()); string fileFullPath = StorageFolder + "\\" + file.Path + "\\" + file.Name; FileStream outputStream = GetFileStream(file.Path, file.Name); try { fileSourceStream.CopyTo(outputStream); fileSourceStream.Close(); } catch (IOException ex)// клиент закрыл поток { logger.Error("Сервер закрыл соединение. " + ex.Message); } outputStream.Close(); FileInfo savedFileInfo = new FileInfo(fileFullPath); logger.Debug("Исходный файл:{0}, Полученный файл:{1}", file.Size, savedFileInfo.Length); if (file.Size != savedFileInfo.Length) { logger.Error("Размер файл полученного файла не соответствует исходному. Полученный файл будет удален!"); } logger.Info("Сделано"); }
//-------------------------------------------------------------------------------- //сохранение файла private void backgroundDownloader_DoWork(object sender, DoWorkEventArgs e) { MyFile file=(MyFile)e.Argument; using (FileStream output = new FileStream(file.Path, FileMode.Create)) { SinchronizeFileProgressInfo.ProgressBytes = 0; SinchronizeFileProgressInfo.ProgressProcent = 0; SinchronizeFileProgressInfo.Action = FileStatus.Download; using (FileSystemClient serverFileSystem = new FileSystemClient()) { StreamWithProgress fileSourceStream = new StreamWithProgress(serverFileSystem.GetFileStream(selectedFileId, Account.GetUserEmail(), Account.GetUserPass())); fileSourceStream.ProgressChanged += SetProgressInfoData; fileSourceStream.CopyTo(output); } } }
private void UploadButton_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; try { // get some info about the input file System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileTextBox.Text); // show start message LogText("Starting uploading " + fileInfo.Name); LogText("Size : " + fileInfo.Length); // open input stream using (System.IO.FileStream stream = new System.IO.FileStream(FileTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream)) { uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged; // start service client FileTransferClient.FileTransferServiceClient client = new FileTransferClient.FileTransferServiceClient(); // upload file client.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress); LogText("Done!"); // close service client client.Close(); } } } catch (Exception ex) { LogText("Exception : " + ex.Message); if (ex.InnerException != null) { LogText("Inner Exception : " + ex.InnerException.Message); } } finally { Cursor = Cursors.Default; } }
private void UploadButton_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; try { // get some info about the input file System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileTextBox.Text); // show start message LogText("Starting uploading " + fileInfo.Name); LogText("Size : " + fileInfo.Length); // open input stream using (System.IO.FileStream stream = new System.IO.FileStream(FileTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream)) { uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged; // start service client FileTransferServiceClient client = new FileTransferServiceClient(); // upload file ResoureMetadataMessage res = client.SaveFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress); LogText("Done!"); // close service client //client.Close(); } } } catch (Exception ex) { LogText("Exception : " + ex.Message); if (ex.InnerException != null) LogText("Inner Exception : " + ex.InnerException.Message); } finally { Cursor = Cursors.Default; } }
private void uploadStreamWithProgress_ProgressChanged(object sender, StreamWithProgress.ProgressChangedEventArgs e) { if (e.Length != 0) progressBar1.Value = (int)(e.BytesRead * 100 / e.Length); }
//отправялет файл на сервер public void UploadFile(MyFile file, StreamWithProgress stream) { serverFileSystem.UploadFile(file, Account.GetUserEmail(), Account.GetUserPass(), stream); }
//синхронизирует файлы private void Sinchronizing(List<MyFile> filesForSinchronize) { if (OnChangeSinchronizeStatus != null) OnChangeSinchronizeStatus(this, SinchronizeStatus.SinchronizeStarted); foreach (MyFile file in filesForSinchronize) { SinchronizeFileProgressInfo.File = file; SinchronizeFileProgressInfo.ProgressBytes = 0; SinchronizeFileProgressInfo.ProgressProcent = 0; SinchronizeFileProgressInfo.Action=file.status; if (OnProcessFileInfo != null) OnProcessFileInfo(this, SinchronizeFileProgressInfo); if (file.status == FileStatus.Delete) { if (file.IsDirectory) { ClientFileSystem.Instance.DeleteDirectory(file.Path); ServerFileSystem.Instance.DeleteDirectory(file.FileId); } else { ServerFileSystem.Instance.DeleteFile(file.FileId); ClientFileSystem.Instance.DeleteFile(file.Path, file.Name); } } if (file.status == FileStatus.Download) { if (file.IsDirectory) ClientFileSystem.Instance.CreateDirectory(file.Path); else { StreamWithProgress fileSourceStream = new StreamWithProgress(ServerFileSystem.Instance.GetFileStream(file.FileId)); fileSourceStream.ProgressChanged += SetProgressInfoData; ClientFileSystem.Instance.DownloadFile(fileSourceStream, file); } } if (file.status == FileStatus.Upload)// если update { if (file.IsDirectory) ServerFileSystem.Instance.CreateDirectory(file.Path); else using (FileStream fileStream = ClientFileSystem.Instance.GetFileStream(file.Path, file.Name)) { StreamWithProgress fileSourceStream = new StreamWithProgress(fileStream); fileSourceStream.ProgressChanged += SetProgressInfoData; file.SizeSpecified = true; ServerFileSystem.Instance.UploadFile(file, fileSourceStream ); } } } if (OnChangeSinchronizeStatus != null) OnChangeSinchronizeStatus(this, SinchronizeStatus.SinchronizeFinished); }
//устанавливает прогресс синхронизации void SetProgressInfoData(object sender, StreamWithProgress.ProgressChangedEventArgs e) { SinchronizeFileProgressInfo.ProgressBytes = e.BytesRead; // количество обработанных байт SinchronizeFileProgressInfo.ProgressProcent = 0; if (e.BytesRead > 0) { float progressProcent = ((float)(e.BytesRead / 1024)) / ((float)(SinchronizeFileProgressInfo.File.Size / 1024)); if (progressProcent<100) SinchronizeFileProgressInfo.ProgressProcent = (int)(progressProcent * 100); // процент обработки } }