FTP File Download

Source: Internet
Author: User
Tags ftp file how to use ftp

 


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!


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.