FTP File Upload
FTP File Download
Delete an FTP File
Create directory for FTP File Operations
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. This is the basic principle. Let's take a look at the specificCode:
/// <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 Uri Reqftp = (ftpwebrequest) ftpwebrequest. Create ( New Uri (ftppath + Filename )); // Specify the command to be executed Reqftp. method = Webrequestmethods. FTP. downloadfile; // 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 Buffersize = 2048 ; Int Readcount; Byte [] Buffer = New Byte [Buffersize]; // 2 kb of each file stream read Readcount = ftpstream. Read (buffer, 0 , Buffersize ); While (Readcount> 0 ){ // Write content from the file stream Outputstream. Write (buffer, 0 , Readcount); readcount = Ftpstream. Read (buffer, 0 , Buffersize );} // Disable two streams and FTP connections Ftpstream. Close (); outputstream. Close (); response. Close ();} Catch (Exception ex) {sret = Ex. Message ;} // Returns 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!