I have previously written an ftp-based file upload method. How can I not download an uploaded file? If only the upload is not downloaded, the upload is useless. So today I will learn how to use ftp to download files.
Once you know how to upload the file, it becomes very easy to download it. Uploading is to put the file on the server, while downloading is to take the file from the server. One is to read the file locally and then write it to the server; the other is to read the file from the server and then write it to the local. The basic principle is as follows. Let's look at the specific code:
[Csharp]
/// <Summary>
/// FTP File Download
/// </Summary>
/// <Param name = "userId"> ftp user name </param>
/// <Param name = "pwd"> ftp password </param>
/// <Param name = "ftpPath"> ftp file path </param>
/// <Param name = "filePath"> download and save path </param>
/// <Param name = "fileName"> ftp file name </param>
/// <Returns> </returns>
Public string Download (string userId, string pwd, string ftpPath, string filePath, string fileName)
{
String sRet = "Download successful! ";
FtpWebRequest reqFTP;
Try
{
FileStream outputStream = new FileStream (filePath + fileName, FileMode. Create );
// Create an FtpWebRequest object based on the uri
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (ftpPath + fileName ));
// Specify the command to be executed
ReqFTP. Method = WebRequestMethods. Ftp. DownloadFile;
// Specify the data transmission type
ReqFTP. UseBinary = true;
ReqFTP. UsePassive = false;
// Ftp user name and password
ReqFTP. Credentials = new NetworkCredential (userId, pwd );
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
// Write the downloaded file to the stream
Stream ftpStream = response. GetResponseStream ();
Long cl = response. ContentLength;
// Set the buffer size to 2 kb.
Int buffer size = 2048;
Int readCount;
Byte [] buffer = new byte [bufferSize];
// 2kb of each file stream read
ReadCount = ftpStream. Read (buffer, 0, bufferSize );
While (readCount> 0)
{
// Write the content from the file stream
OutputStream. Write (buffer, 0, readCount );
ReadCount = ftpStream. Read (buffer, 0, bufferSize );
}
// Close two streams and ftp connections
FtpStream. Close ();
OutputStream. Close ();
Response. Close ();
}
Catch (Exception ex)
{
SRet = ex. Message;
}
// Return the download result (whether the download is successful)
Return sRet;
}
/// <Summary>
/// FTP File Download
/// </Summary>
/// <Param name = "userId"> ftp user name </param>
/// <Param name = "pwd"> ftp password </param>
/// <Param name = "ftpPath"> ftp file path </param>
/// <Param name = "filePath"> download and save path </param>
/// <Param name = "fileName"> ftp file name </param>
/// <Returns> </returns>
Public string Download (string userId, string pwd, string ftpPath, string filePath, string fileName)
{
String sRet = "Download successful! ";
FtpWebRequest reqFTP;
Try
{
FileStream outputStream = new FileStream (filePath + fileName, FileMode. Create );
// Create an FtpWebRequest object based on the uri
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (ftpPath + fileName ));
// Specify the command to be executed
ReqFTP. Method = WebRequestMethods. Ftp. DownloadFile;
// Specify the data transmission type
ReqFTP. UseBinary = true;
ReqFTP. UsePassive = false;
// Ftp user name and password
ReqFTP. Credentials = new NetworkCredential (userId, pwd );
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
// Write the downloaded file to the stream
Stream ftpStream = response. GetResponseStream ();
Long cl = response. ContentLength;
// Set the buffer size to 2 kb.
Int buffer size = 2048;
Int readCount;
Byte [] buffer = new byte [bufferSize];
// 2kb of each file stream read
ReadCount = ftpStream. Read (buffer, 0, bufferSize );
While (readCount> 0)
{
// Write the content from the file stream
OutputStream. Write (buffer, 0, readCount );
ReadCount = ftpStream. Read (buffer, 0, bufferSize );
}
// Close two streams and ftp connections
FtpStream. Close ();
OutputStream. Close ();
Response. Close ();
}
Catch (Exception ex)
{
SRet = ex. Message;
}
// Return the download result (whether the download is successful)
Return sRet;
}
The above code can implement a simple ftp download function, as long as you call this method as needed. The code is very simple and the function is very practical.
There are also many FTP-related operations that will be shared with you in the future. Stay tuned!