1. Category:
Indicates the type of FTP protocol method that can be used with FTP requests.
- AppendFile: Indicates the FTP APPE protocol method to be used to append files to existing files on the FTP server.
- DeleteFile: Indicates the FTP DELE protocol method to use to delete files on the FTP server.
- DownloadFile: Indicates the FTP RETR protocol method to be used to download files from an FTP server.
- GetDateTimestamp: Indicates the FTP MDTM protocol method to be used to retrieve date timestamps from files on the FTP server.
- GetFileSize: Indicates the FTP SIZE protocol method to use to retrieve file sizes on the FTP server.
- ListDirectory: The FTP NLIST protocol method that represents the FTP NLIST protocol method that obtains a short list of files on the FTP server.
- ListDirectoryDetails: The FTP LIST protocol method that represents the FTP LIST protocol that obtains a detailed list of files on the FTP server.
- MakeDirectory: Represents the FTP MKD protocol method that creates a directory on the FTP server.
- PrintWorkingDirectory: The FTP PWD protocol method that represents the name of the current working directory.
- RemoveDirectory: Indicates the FTP RMD protocol method that removes the directory.
- Rename: represents the FTP RENAME protocol method of renamed directories.
- UploadFile: Indicates the FTP STOR protocol method that uploads the file to the FTP server.
- UploadFileWithUniqueName: Indicates the FTP STOU protocol method that uploads files with unique names to the FTP server.
2. Upload the file:
OpenFileDialog opFilDlg = new OpenFileDialog(); if (() == ) { ftp = new YBBFTPClass("", "", "csp", "welcome", 0); (); ("Uploaded successfully"); }
/// <summary> /// Upload file/// </summary> /// <param name="filename">local file path</param>public void UploadFile(string filename) { FileInfo fileInf = new FileInfo(filename); string uri = "ftp://" + RemoteHost + "/" + ; FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)(new Uri("ftp://" + RemoteHost + "/" + ));// Create FtpWebRequest object based on uri = new NetworkCredential(RemoteUser, RemotePass); // ftp username and password = false; // Default is true, the connection will not be closed and executed after a command = ; // Specify what command to execute = true; // Specify the data transfer type = ; // Notify the server file size when uploading int contentLen; FileStream fileStream = (); // Open a file to read the content into fileStream contentLen = (buffer, 0, ); ;//Read data from fileStream into buffer Stream requestStream = (); // The streaming content has not ended while (contentLen != 0) { (buffer, 0, contentLen);// Write the content from the buffer to the requestStream and complete the upload. contentLen = (buffer, 0, ); } // Close two streams (); //uploadResponse = (FtpWebResponse)(); (); }
3. Download the file
1. Core code
/// <summary> /// Download the file/// </summary> /// <param name="filePath">Local Directory</param>/// <param name="fileName">Remote Path</param>public void DownloadFile(string filePath, string fileName) { FtpWebRequest reqFTP; try { FileStream fileStream = new FileStream(filePath + "\\" + fileName, ); reqFTP = (FtpWebRequest)(new Uri("ftp://" + RemoteHost + "/" + fileName)); = ; = true; = new NetworkCredential(RemoteUser, RemotePass); FtpWebResponse response = (FtpWebResponse)(); Stream responseStream = ();//Get response stream from ftp response //long cl = ; byte[] buffer = new byte[1024]; int readCount; readCount = (buffer, 0, );//Read data from ftp's responseStream into buffer while (readCount > 0) { (buffer, 0, readCount);//Read data from buffer into fileStream and complete download readCount = (buffer, 0, ); } (); (); (); } catch (Exception ex) { (); } }
2、winform:
FolderBrowserDialog fldDlg = new FolderBrowserDialog(); if (().Length > 0) { if (() == ) { (, ()); ("Download successfully"); } } else { ("Please enter the File name to download"); }
3. Webform pop-up download prompt:
FtpClient _client = new FtpClient(); Stream stream = _client.OpenRead(FtpFilePath, ); string FtpFilePath = ["FilePath"]; string _fname = (FtpFilePath); = "application/" + _fname.Split('.')[1]; ("Content-disposition", "attachment; filename=" + _fname); byte[] buffer = new byte[10240]; int readCount; do { readCount = (buffer, 0, ); (buffer, 0, readCount);//Continuous writing to stream} while (readCount != 0); (buffer, 0, ); ();
4. Delete files
string uri = "ftp://" + RemoteHost + "/" + fileName; FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)(new Uri("ftp://" + RemoteHost + "/" + fileName)); = new NetworkCredential(RemoteUser, RemotePass); = false; = ; string result = ; FtpWebResponse response = (FtpWebResponse)(); long size = ; Stream datastream = (); StreamReader sr = new StreamReader(datastream); result = (); (); (); ();
Complete code reference:
https:///article/
This is the end of this article about C# using FtpWebRequest and FtpWebResponse to complete FTP operations. I hope it will be helpful to everyone's learning and I hope everyone will support me more.