By Bruce Content on this page Introduction Log on to the FTP server Get the Directory and file list Active and passive Modes Directory operations Upload and download files Resumable upload Delete an object Introduction File transfer protocol is a software standard for transferring computer files between two machines with different operating systems. It belongs to the application layer of the network protocol group. FTP is an 8-bit client-server protocol that can operate on any type of files without further processing, just like MIME or Unencode. The FTP service generally runs on two ports. Port 21 is used to transmit the control flow and the command is used to import to the ftp server. Another port is used to transmit data streams between the client and the server. The port number is automatically allocated by the system. Using the FTP protocol to transfer files has the following advantages: 1. Promote file sharing (computer programs or data) 2. Encourage indirect or implicit use of remote computers 3. shield users from the details of various file storage systems on different hosts 4. reliable and efficient data transmission Under the namespace Eastasp. Framework. Net. Ftp, Snake. Net provides a series of classes and functions to provide developers with comprehensive and complete file transfer functions using the FTP protocol. This section describes how FtpClient and FtpExpress can send and transmit files. Log on to the FTP server FTP usually requires a user name and password to log on, or Anonymous logon can be used. At this time, Anonymous is generally used as the user name to log on, and the user's Email address is used as the password. See the Demo code. // Declare FtpServer server; FtpClient client;Server = new FtpServer ("ftp.server.com", "user", "password "); Client = new FtpClient (server ); Try { Client. Open (); Client. Noop (); } Finally { If (client! = Null ){ Client. Close (); client = null; } } Get the Directory and file list Folders and files are managed on the FTP server according to certain rules, just like local folders. Generally, a large number of files are stored on the FTP server and placed according to certain rules. The user must browse the Directory and select the desired file for download. FtpClient provides the List command to List folder river files on the server. See the Demo code: // Declare FtpServer server; FtpClient client; String remotePath; FtpPoint point; FtpFileInfo [] files; FtpDirectoryInfo [] directories;RemotePath = ""; Server = new FtpServer ("ftp.server.com", "user", "password "); Client = new FtpClient (server ); Try { Client. Open (); Client. Type (FtpTransferType. ASCII ); Point = client. List (remotePath ); // Get directories Directories = point. Directories; If (directories. Length> 0 ){ For (int I = 0; I <directories. Length; I ++ ){ Console. WriteLine (directories [I]. Name ); } } // Get files Files = point. Files; If (files. Length> 0 ){ For (int I = 0; I <files. Length; I ++ ){ Console. WriteLine (files [I]. Name ); } } } Finally { If (client! = Null ){ Client. Close (); client = null; } } For different FTP server software, the formats of FTP directory output are also different. There are mainly two types of Unix and MSDOS. Currently, Most FTP servers use the Unix format. In general, only FTP under IIS may be in the MSDOS format, you can set the Type attribute of the FtpServer object to MSDOS mode. See the following code: // Declare FtpServer server;Server = new FtpServer ("ftp.server.com", "user", "password "); Server. Type = FtpServerType. MsDos; Active and passive Modes FTP has two usage modes: active and passive. In active mode, both the client and the server are required to open and listen to a port to establish a connection. In this case, the client may have some problems due to the firewall installation. Therefore, the passive mode is created. In passive mode, the server is only required to generate a process that listens to the corresponding port, so that the firewall can be installed on the client. The active and passive modes correspond to the FtpConnectionType enumerated Port and Pasv respectively in Snake. Net. You can set it through the ConnectionType attribute of FtpClient. // Declare FtpClient client; Client. ConnectionType = FtpConnectionType. Port;Directory operations You can create, delete, and rename directories on the Ftp server just like the local directory. FtpClient provides the following commands for operating the directory where the server is disconnected. Pwd: Get the current directory location information Cwd sets the specified directory to the current directory Mkd creates a specified directory in the current directory Rmd deletes the specified directory under the current directory Move to change the directory name. This command is also valid for files. // Declare FtpClient client;// Initialize Client = new FtpClient (new FtpServer ("ftp.server.com", "user", "password ")); Client. ConnectionType = FtpConnectionType. Port; Try { Client. Open (); Client. Noop (); // Changes the current directory Client. Cwd ("/"); Client. Type (FtpTransferType. ASCII ); // Create directory t Client. Mkd ("t "); // Changes the current directory Client. Cwd ("t "); // Output current directory info Console. WriteLine (client. Pwd ()); // Create directory aa and bb Client. Mkd ("aa"); client. Mkd ("bb "); // Rename directory aa to cc Client. Move ("aa", "cc "); // Delete directory bb and cc Client. Rmd ("bb"); client. Rmd ("cc "); // Go up directory Client. Cwd (".."); // Delete directory t Client. Rmd ("t "); } Finally { If (client! = Null ){ Client. Close (); client = null; } } Upload and download files Since FTP is a File Transfer Protocol, uploading and downloading are of course the most important functions. FtpClient provides the Store Method for uploading files, and the Retrieve Method for downloading files. Take a look at the uploaded DEMO code: // Declare FtpClient client; String localFile;LocalFile = @ "c: \ a. dat "; Client = new FtpClient (new FtpServer ("ftp.server.com", "user", "password ")); Client. ConnectionType = FtpConnectionType. Port; Try { Client. Open (); client. Noop (); Client. Type (FtpTransferType. Image ); Client. Store (localFile ); } Finally { If (client! = Null ){ Client. Close (); client = null; } } Next, let's take a look at the Demo code for file download: // Declare FtpClient client; String localFile, remoteFile;RemoteFile = "a. dat "; LocalFile = string. Format (@ "c: \ 2 {0}", remoteFile ); Client = new FtpClient (new FtpServer ("ftp.server.com", "user", "password ")); Client. ConnectionType = FtpConnectionType. Port; Try { Client. Open (); client. Noop (); Client. Type (FtpTransferType. Image ); Client. Retrieve (localFile, remoteFile ); } Finally { If (client! = Null ){ Client. Close (); client = null; } } Resumable upload Sometimes it takes several hours for a user to upload or download files through FTP. If the line is interrupted, an FTP server that does not have an FTP resumable data transfer can only be re-transmitted from scratch; for servers with the resumable data transfer function, users can continue to transfer data from the last interrupted location, instead of starting from scratch. FtpClient uses the Rest command to set breakpoints. The Store and Retrieve methods of FtpClient also support file breakpoint support. See the following code: // Set the breakpoint Client. Rest (1000 );// Upload a file Client. Store (localFile ); // Upload 1,000th bytes from the file Client. Store (localFile, 1000 ); The Store and Retrieve methods provide different parameters for file upload and download in various situations. Delete an object Deleting a file on a remote Ftp server is simple. You only need to call the Delete command of FtpClient. Please refer to the Code: // Declare FtpClient client; String remoteFile;RemoteFile = "a. dat "; Client = new FtpClient (new FtpServer ("ftp.server.com", "user", "password ")); Client. ConnectionType = FtpConnectionType. Port; Try { Client. Open (); client. Noop (); Client. Type (FtpTransferType. Image ); Client. Delete (remoteFile ); } Finally { If (client! = Null ){ Client. Close (); client = null; } } |