Using FTP auxiliary classes to upload or download files in the development framework to facilitate the management of attachment content

Keywords: C# ftp Database Unix

In some system applications, we need to separate application servers, database servers, file servers, file paths and other information stored in the database server, but file content is stored in the file server, using FTP file upload and download, so as to achieve more efficient IO output, but also to share the pressure of the server. This article introduces the basic operation of uploading and downloading files by using FTPHelper auxiliary classes in the common class library.

1. Interface Description of FTPHelper Auxiliary Class

First, in order to facilitate the use of auxiliary classes, we use constructors to initialize the parameters of the object, as shown below.

Our entire FTPHelper auxiliary class uses FTP commands to exchange with FTP servers, so if we want to obtain file or directory information, then we need to convert these information into content in the appropriate format. Here we define a structure to carry this information.

In this way, we can smoothly return the FTP server strings (DOS or Unix format strings) to the corresponding structure, which is easy to use.

 

The whole auxiliary function provides some basic functions to manipulate FTP files or directories, as shown below.

        /// <summary>
        /// Get only the list of files
        /// </summary>
        /// <returns>Get a list of files</returns>
        public List<FileStruct> ListFiles()
        
        /// <summary>
        /// Get directory lists only
        /// </summary>
        /// <returns>Get a list of directories</returns>
        public List<FileStruct> ListDirectories()    

as well as

        /// <summary>
        /// Close FTP All connections to the server
        /// </summary>
        public void Disconnect()

        /// <summary>
        /// connection to FTP The server
        /// </summary>
        /// <param name="server">FTP Server IP Or host name</param>
        /// <param name="port">FTP Server Port</param>
        /// <param name="user">Username</param>
        /// <param name="pass">Login password</param>
        public void Connect(string server, int port, string user, string pass)        
        
        /// <summary>
        /// Obtain FTP Current working directory
        /// </summary>
        public string GetWorkingDirectory()        
        
        /// <summary>
        /// stay FTP Change directories on servers
        /// </summary>
        public void ChangeDir(string path)        
        
        /// <summary>
        /// stay FTP Create a new directory on the server
        /// </summary>
        public void MakeDir(string dir)        
        
        /// <summary>
        /// stay FTP Remove directories from servers
        /// </summary>
        public void RemoveDir(string dir)    
        
        /// <summary>
        /// stay FTP Remove a file from it
        /// </summary>
        public void RemoveFile(string filename)        
        
        /// <summary>
        /// stay FTP Rename a file on the server
        /// </summary>
        /// <param name="oldfilename">Old file name</param>
        /// <param name="newfilename">New file name</param>
        public void RenameFile(string oldfilename, string newfilename)

        /// <summary>
        /// Open an existing file to upload
        /// </summary>
        /// <param name="filename">Locally uploaded files (including file paths)</param>
        /// <param name="remote_filename">store in FTP File name on</param>
        /// <param name="resume">If it exists, specify whether to continue</param>
        public void OpenUpload(string filename, string remote_filename, bool resume)

        /// <summary>
        /// Open and download a file
        /// </summary>
        /// <param name="remote_filename">FTP Upper remote filename</param>
        /// <param name="local_filename">File name saved as(Contains file paths)</param>
        /// <param name="resume">If it exists, specify whether to continue</param>
        public void OpenDownload(string remote_filename, string local_filename, bool resume)    
        
        /// <summary>
        /// Upload files, loop until all files are uploaded
        /// </summary>
        /// <returns>The size of the byte array sent</returns>
        public long DoUpload()
        
        /// <summary>
        /// Download the file and cycle until all the files have been downloaded.
        /// </summary>
        /// <returns>Received byte array size</returns>
        public long DoDownload()
        

2. The Use of FTPHelper Auxiliary Class

Based on the interface mentioned above, we have written a Demo, which is mainly used to introduce the use of various interfaces of this auxiliary class.

The output of the obtained test results is as follows

Although we adopt a more general annex management module in the annex management, which unifies uploading files to the server for management, and provides download management and other content, the links are as follows:

<Appendix Management Application of Winform Development Framework " The usage instructions of other public class libraries can be referred to in the article.< Common Class Library Usage Help>

However, when we develop, we can also use FTPHelper auxiliary class for simple file upload and download operation for convenience.

Posted by DuNuNuBatman on Sun, 21 Apr 2019 16:00:34 -0700