protected bool TransferData(Stream dataStream, Stream requestStream) { if (dataStream.CanSeek) { dataStream.Position = 0; } ProgressManager progress = new ProgressManager(dataStream.Length); int length = (int)Math.Min(BufferSize, dataStream.Length); byte[] buffer = new byte[length]; int bytesRead; while (!StopUploadRequested && (bytesRead = dataStream.Read(buffer, 0, length)) > 0) { requestStream.Write(buffer, 0, bytesRead); if (AllowReportProgress && progress.UpdateProgress(bytesRead)) { OnProgressChanged(progress); } } return(!StopUploadRequested); }
private void bw_AsyncUploadDoWork(object sender, DoWorkEventArgs e) { try { AsyncUploadHelper upload = (AsyncUploadHelper)e.Argument; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(upload.URL); request.Proxy = Options.ProxySettings; request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(Options.Account.Username, Options.Account.Password); request.KeepAlive = false; request.UsePassive = !Options.Account.IsActive; using (upload.Stream) using (Stream requestStream = request.GetRequestStream()) { ProgressManager progress = new ProgressManager(upload.Stream.Length); byte[] buffer = new byte[BufferSize]; int bytesRead; while ((bytesRead = upload.Stream.Read(buffer, 0, BufferSize)) > 0) { requestStream.Write(buffer, 0, bytesRead); progress.UpdateProgress(bytesRead); upload.BackgroundWorker.ReportProgress((int)progress.Percentage, progress); } } } catch (Exception ex) { DebugHelper.WriteException(ex); } }
protected bool TransferData(Stream dataStream, Stream requestStream, long dataPosition = 0, long dataLength = -1) { if (dataPosition >= dataStream.Length) { return(true); } if (dataStream.CanSeek) { dataStream.Position = dataPosition; } if (dataLength == -1) { dataLength = dataStream.Length; } dataLength = Math.Min(dataLength, dataStream.Length - dataPosition); ProgressManager progress = new ProgressManager(dataStream.Length, dataPosition); int length = (int)Math.Min(BufferSize, dataLength); byte[] buffer = new byte[length]; int bytesRead; long bytesRemaining = dataLength; while (!StopUploadRequested && (bytesRead = dataStream.Read(buffer, 0, length)) > 0) { requestStream.Write(buffer, 0, bytesRead); bytesRemaining -= bytesRead; length = (int)Math.Min(buffer.Length, bytesRemaining); if (AllowReportProgress && progress.UpdateProgress(bytesRead)) { OnProgressChanged(progress); } } return(!StopUploadRequested); }
public bool DownloadFile(string url, string savePath) { try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.Proxy = Options.ProxySettings; request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential(Options.Account.Username, Options.Account.Password); request.KeepAlive = false; request.UsePassive = !Options.Account.IsActive; using (FileStream fileStream = new FileStream(savePath, FileMode.Create)) using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) { ProgressManager progress = new ProgressManager(stream.Length); byte[] buffer = new byte[BufferSize]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, BufferSize)) > 0) { fileStream.Write(buffer, 0, bytesRead); progress.UpdateProgress(bytesRead); OnProgressChanged(progress); } } WriteOutput(string.Format("DownloadFile: {0} -> {1}", url, savePath)); return(true); } catch (Exception ex) { WriteOutput(string.Format("Error: {0} - DownloadFile: {1} -> {2}", ex.Message, url, savePath)); } return(false); }
public bool Upload(Stream stream, string url) { try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.Proxy = Options.ProxySettings; request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(Options.Account.Username, Options.Account.Password); request.KeepAlive = false; request.UsePassive = !Options.Account.IsActive; using (stream) using (Stream requestStream = request.GetRequestStream()) { ProgressManager progress = new ProgressManager(stream.Length); byte[] buffer = new byte[BufferSize]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, BufferSize)) > 0) { requestStream.Write(buffer, 0, bytesRead); progress.UpdateProgress(bytesRead); OnProgressChanged(progress); } } WriteOutput("Upload: " + url); return true; } catch (Exception ex) { WriteOutput(string.Format("Error: {0} - Upload: {1}", ex.Message, url)); } return false; }
protected bool TransferData(Stream dataStream, Stream requestStream) { if (dataStream.CanSeek) { dataStream.Position = 0; } ProgressManager progress = new ProgressManager(dataStream.Length); int length = (int)Math.Min(BufferSize, dataStream.Length); byte[] buffer = new byte[length]; int bytesRead; while (!StopUploadRequested && (bytesRead = dataStream.Read(buffer, 0, length)) > 0) { requestStream.Write(buffer, 0, bytesRead); if (AllowReportProgress && progress.UpdateProgress(bytesRead)) { OnProgressChanged(progress); } } return !StopUploadRequested; }