/// <summary> /// Iniatlize the FtpTransferInfo object /// </summary> /// <param name="type">Upload or download</param> /// <param name="file">Remote object path</param> /// <param name="length">Size of the object in bytes</param> /// <param name="resume">Bytes resumed (if this was a resume)</param> /// <param name="transferred">Bytes transfered</param> /// <param name="start">The time the transfer started</param> /// <param name="complete">Value indicating if the transfer is complete</param> public FtpTransferInfo(FtpTransferType type, string file, long length, long resume, long transferred, DateTime start, bool complete) { this.TransferType = type; this.FileName = file; this.Length = length; this.Resume = resume; this.Transferred = transferred; this.Start = start; this.Complete = complete; }
public FtpClient(string serverAddress, int port, string userName, string password, bool passive = false, bool ascii = false, ILogger logger = null) { _serverAddress = serverAddress; _port = port; _userName = userName; _password = password; _passive = passive; _transferType = ascii ? FtpTransferType.Ascii : FtpTransferType.Binary; _logger = logger; RetryCount = 0; }
/// <summary> /// REPRESENTATION TYPE. /// </summary> /// <remarks> /// The argument specifies the representation type as described /// in the Section on Data Representation and Storage. Several /// types take a second parameter. The first parameter is /// denoted by a single Telnet character, as is the second /// Format parameter for ASCII and EBCDIC; the second parameter /// for local byte is a decimal integer to indicate Bytesize. /// The parameters are separated by a (SP) (Space, ASCII code 32). /// /// The default representation type is ASCII Non-print. If the /// Format parameter is changed, and later just the first /// argument is changed, Format then returns to the Non-print /// default. /// </remarks> /// <param name="args">The arguments.</param> void TYPE(string args) { if (!CheckLogin()) { return; } FtpTransferType typeCode = (FtpTransferType)args.FirstOrDefault(); switch (typeCode) { //implemented types: case FtpTransferType.ASCII: case FtpTransferType.IMAGE: TransferType = typeCode; FormatControl = FtpFormatControl.NONPRINT; break; //not implemented types: default: SendAnswer("504 TypeCode not implemented"); return; } //The types ASCII and EBCDIC also take a second (optional) parameter; this is to indicate what kind of vertical format //control, if any, is associated with a file. The following data representation types are defined in FTP: string optional = args.AfterFirst(' '); if (optional.Length > 0) { FtpFormatControl newFormatControl = (FtpFormatControl)optional.FirstOrDefault(); switch (newFormatControl) { case FtpFormatControl.NONPRINT: FormatControl = FtpFormatControl.NONPRINT; break; default: SendAnswer($"504 FormatControl {newFormatControl} not implemented for TransferType {TransferType}."); return; } } SendAnswer($"200 OK TransferType {TransferType} FormatControl {FormatControl}"); }
/// <summary> /// Constructor /// </summary> public FtpClient() { _ConnectMode = FtpConnectionMode.ACTIVE; _TransferType = FtpTransferType.BINARY; _KeepAlive = true; }
/// <summary> /// Sends a file to the FTP server /// </summary> /// <param name="filename">Full path to the file to send</param> /// <returns>True if file is sent</returns> public bool SendFile(string filename, FtpTransferType type) { if(type.Equals(FtpTransferType.ASCII)) SendCommand("TYPE A"); else SendCommand("TYPE I"); if(_rspCode != 200) return false; Socket data = OpenDataSocket(); if(data == null) { _rspCode = -1; _rspMessage = Properties.Resources.ErrorFtpOpenSocket; return false; } SendCommand("STOR " + Path.GetFileName(filename)); if(_rspCode != 125 && _rspCode != 150) return false; try { FileStream fs = new FileStream(filename, FileMode.Open); byte[] buffer = new byte[BLOCKSIZE]; int bytes; while((bytes = fs.Read(buffer, 0, BLOCKSIZE)) > 0) { data.Send(buffer, bytes, 0); } fs.Close(); data.Close(); GetResponse(); if(_rspCode != 226 && _rspCode != 250) return false; return true; } catch { _rspCode = -1; _rspMessage = Properties.Resources.ErrorFtpReadFile; return false; } }