C # ftp upload and download

Source: Internet
Author: User

To test whether the code we wrote has the ftp upload/download function, we need to download a virtual FTP server, such as Serv-U. After configuring Serv-U, we can test the encoding.
Introduce namespace: using System. Net;

////// Download the object //////Path of the downloaded file///Name of the downloaded file///File name to download///
 Private int Download (string filePath, string SaveFileName, string parFileName) {FtpWebRequest reqFTP; FileStream outputStream = new FileStream (filePath + "\" + SaveFileName, FileMode. create); try {reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri ("ftp: //" + strFTPIP + "/" + strFtpDownPath + "/" + parFileName); reqFTP. method = WebRequestMethods. ftp. downloadFile; reqFTP. useBinary = true; reqFT P. credentials = new NetworkCredential (strFTPUser, strFTPPSW); FtpWebResponse response = (FtpWebResponse) reqFTP. getResponse (); Stream ftpStream = response. getResponseStream (); long cl = response. contentLength; int bufferSize = 2048; int readCount; byte [] buffer = new byte [bufferSize]; readCount = ftpStream. read (buffer, 0, bufferSize); while (readCount> 0) {outputStream. write (buffer, 0, readCount); ReadCount = ftpStream. Read (buffer, 0, bufferSize);} // After the download is successful, delete the files on the server. // FtpWebRequest request = (FtpWebRequest) WebRequest. create (new Uri ("ftp: //" + strFTPIP + "/" + strFtpPath + "/" + fileName); // request. method = WebRequestMethods. ftp. deleteFile; // FtpWebResponse responseD = (FtpWebResponse) request. getResponse (); ftpStream. close (); outputStream. close (); response. close (); return readCount;} catch (Exception ex) {outputStream. close (); // MessageBox. show (ex. message); LogAPI. writeLog (ex. message); return-1 ;}}////// Method to upload the specified file to the specified FTP Server //////File full name to be uploadedPrivate bool Upload (string filePath, string filename) {FileInfo fileInf = new FileInfo (filename); string uri = "ftp: // "+ strFTPIP +"/"+ filePath +"/"+ fileInf. name; FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri ("ftp: //" + strFTPIP + "/" + filePath + "/" + fileInf. name); // Provide the WebPermission Credintials ReqFTP. credentials = new NetworkCredential (strFTPUser, strFTPPSW); // By default KeepAlive is true, where the control connection is not closed // after a command is executed. reqFTP. keepAlive = false; // Specify the command to be executed. reqFTP. method = WebRequestMethods. ftp. uploadFile; // Specify the data transfer type. reqFTP. useBinary = true; // configure y the server about the size of the uploade D file reqFTP. contentLength = fileInf. length; // The buffer size is set to 2kb int buffLength = 2048; byte [] buff = new byte [buffLength]; int contentLen; // Opens a file stream (System. IO. fileStream) to read the file to be uploaded using (FileStream fs = fileInf. openRead () {try {// Stream to which the file to be upload is written using (Stream strm = reqFTP. getRequestStream () {// Read from th E file stream 2kb at a time contentLen = fs. Read (buff, 0, buffLength); // Till Stream content ends while (contentLen! = 0) {// Write Content from the file stream to the FTP Upload Stream strm. write (buff, 0, contentLen); contentLen = fs. read (buff, 0, buffLength);} // Close the file stream and the Request Stream // strm. close (); // fs. close () ;}} catch (Exception ex) {// fs. close (); LogAPI. writeLog ("Error in getting the Upload! "+ Ex. Message); return false;} return true ;}


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.