void OnTransferProgress(FtpTransferInfo e) { Console.Write("\r{0}/{1} {2}% {3}/s ", e.Transferred, e.Length, e.Percentage, e.BytesPerSecond); if (e.Complete) { Console.WriteLine(); } }
/// <summary> /// Uploads a stream to the specified remote file /// </summary> /// <param name="istream"></param> /// <param name="remote"></param> /// <param name="datatype"></param> /// <param name="rest"></param> /// <example> /// <code source="..\Examples\Upload\Program.cs" lang="cs"></code> /// </example> public void Upload(Stream istream, FtpFile remote, FtpDataType datatype, long rest) { long size = 0; long total = 0; int read = 0; if (istream == null) { throw new ArgumentException("istream is null"); } if (remote == null) { throw new ArgumentException("remote is null"); } if (!istream.CanRead) { throw new ArgumentException("istream is not readable"); } if (istream.CanSeek) { size = istream.Length; if (rest > 0) { // set resume position istream.Seek(rest, SeekOrigin.Begin); total = rest; } } else { rest = 0; } using (FtpDataStream ch = this.OpenWrite(remote.FullName, datatype, rest)) { byte[] buf = new byte[ch.SendBufferSize]; DateTime start = DateTime.Now; FtpTransferInfo e; while ((read = istream.Read(buf, 0, buf.Length)) > 0) { ch.Write(buf, 0, read); total += read; e = new FtpTransferInfo(FtpTransferType.Upload, remote.FullName, size, rest, total, start, false); this.OnTransferProgress(e); if (e.Cancel) { break; } } // fire one more time to let event handler know the transfer is complete this.OnTransferProgress(new FtpTransferInfo(FtpTransferType.Upload, remote.FullName, size, rest, total, start, true)); } }
/// <summary> /// Fires the TransferProgress event /// </summary> /// <param name="e"></param> public void OnTransferProgress(FtpTransferInfo e) { if (_transfer != null) { _transfer(e); } }
/// <summary> /// Downloads the remote file to the specified stream /// </summary> /// <param name="remote"></param> /// <param name="ostream"></param> /// <param name="datatype"></param> /// <param name="rest"></param> /// <example> /// <code source="..\Examples\Download\Program.cs" lang="cs"></code> /// </example> public void Download(FtpFile remote, Stream ostream, FtpDataType datatype, long rest) { long size = remote.Length; long total = 0; int read = 0; if (remote == null) { throw new ArgumentException("remote is null"); } if (ostream == null) { throw new ArgumentException("ostream is null"); } if (!ostream.CanWrite) { throw new ArgumentException("ostream is not writable"); } if (rest > 0 && ostream.CanSeek) { // set reset position ostream.Seek(rest, SeekOrigin.Begin); total = rest; } else if (!ostream.CanSeek) { rest = 0; } try { using (FtpDataStream ch = this.OpenRead(remote.FullName, datatype, rest)) { byte[] buf = new byte[ch.ReceiveBufferSize]; DateTime start = DateTime.Now; FtpTransferInfo e = null; while ((read = ch.Read(buf, 0, buf.Length)) > 0) { ostream.Write(buf, 0, read); total += read; e = new FtpTransferInfo(FtpTransferType.Download, remote.FullName, size, rest, total, start, false); this.OnTransferProgress(e); if (e.Cancel) { break; } } // fire one more time to let event handler know that the transfer is complete this.OnTransferProgress(new FtpTransferInfo(FtpTransferType.Download, remote.FullName, size, rest, total, start, true)); } } finally { ostream.Flush(); } }