You can use FTP-assisted classes to upload or download files in the development framework to facilitate the management of attachment content.
In some system applications, we need to separate the application server, database server, and file server, and store file paths and other information in the database server, but the file content is stored in the file server, by uploading and downloading files through FTP, I/O output is more efficient, and the pressure on the server is also shared. This article describes how to use the FTPHelper helper class in the public class library to perform basic operations such as file upload and download.
1. FTPHelper auxiliary class Interface Description
First, to facilitate the use of helper classes, we use constructors to initialize object parameters, as shown below.
Our entire FTPHelper helper class is to use FTP commands to exchange with the FTP server. Therefore, if you want to obtain file or directory information, you need to convert the information to the appropriate format, here we define a struct to carry this information.
In this way, we can smoothly convert the strings (DOS or Unix strings) returned by the FTP server to the corresponding struct for ease of use.
The auxiliary function provides some basic functions to operate FTP files or directories, as shown below:
/// <Summary> /// obtain only the file List /// </summary> /// <returns> obtain the file List </returns> public List <FileStruct> ListFiles () /// <summary> /// only obtain the directory List /// </summary> /// <returns> the List of retrieved directories </returns> public List <FileStruct> listDirectories ()
And
/// <Summary> /// close all connections to the FTP server /// </summary> public void Disconnect () /// <summary> /// connect to the FTP server /// </summary> /// <param name = "server"> IP address or Host name of the FTP server </param> /// <param name = "port"> FTP server port </param> /// <param name = "user"> logon username </param> /// <param name = "pass"> logon password </param> public void Connect (string server, int port, string user, string pass) /// <summary> /// obtain the current FTP working directory /// </summary> public string GetWorkingDirectory () /// <summary> /// change the directory on the FTP server /// </summary> public void ChangeDir (string path) /// <summary> /// create a new directory on the FTP server // </summary> public void MakeDir (string dir) /// <summary> /// remove the directory from the FTP server /// </summary> public void RemoveDir (string dir) /// <summary> /// remove a file from FTP /// </summary> public void RemoveFile (string filename) /// <summary> /// rename a file on the FTP 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 (supporting continuous transmission) upload /// </summary> /// <param name = "filename"> locally uploaded files (including file paths) </param> /// <param name = "remote_filename"> name of the file stored on FTP </param> /// <param name = "resume"> If yes, specify whether to resume </param> public void OpenUpload (string filename, string remote_filename, bool resume) /// <summary> /// open and download a file /// </summary> /// <param name = "remote_filename"> remote file name on FTP </param> /// <param name = "local_filename"> name of the saved file (including the file path) </param> // <param name = "resume"> If yes, specify whether to resume </param> public void OpenDownload (string remote_filename, string local_filename, bool resume) /// <summary> /// upload a file, loop until all files are uploaded // </summary> /// <returns> size of the byte array sent </returns> public long DoUpload () /// <summary> /// download the object, loop until all files are downloaded /// </summary> /// <returns> size of the received byte array </returns> public long DoDownload ()
2. Use of the FTPHelper helper class
Based on the above interface, we have compiled a Demo to introduce the use of various interfaces of this helper class.
The output of the obtained test results is as follows:
Although we have adopted the more general "attachment management module" in the attachment management, uniformly uploading files to the server for management, and providing download management and other content, the connection is as follows:
Winform development framework attachment management application. For more information about how to use other public class libraries, see public class library help.
However, during development, you can also use the FTPHelper helper class to upload and download simple files for convenience.