C # ftp file folder operation upload/download
Recently I have studied FTP file and folder operations using code.
Obtain the FTP connection:
Ftpwebrequest reqftp = (ftpwebrequest) ftpwebrequest. Create (PATH );
// Specifies the data transmission type. Optional values: false (file type) and true (binary type ).
Reqftp. usebinary = true;
// FTP user name and password
Reqftp. Credentials = new networkcredential (ftpuserid, ftppassword );
File Upload
/// <Summary>
/// Upload a file
/// </Summary>
/// <Param name = "FILENAME"> </param>
Public void upload (string filename, string directoryname)
{
Fileinfo fileinf = new fileinfo (filename );
String uri = "ftp: //" + ftpserverip + "//" + directoryname + "//" + fileinf. Name;
// Get the connection
Connect (URI );
// The default value is true, and the connection will not be closed
// Executed after a command
Reqftp. keepalive = false;
// Specify the command to be executed
Reqftp. method = webrequestmethods. FTP. uploadfile;
// Specify the file size
Reqftp. contentlength = fileinf. length;
// Set the cache size to kb.
Int bufflength = 2048;
Byte [] buff = new byte [bufflength];
Int contenlen;
// Open a file stream (system. Io. filestring) to read the uploaded file
Filestream FS = fileinf. openread ();
Try
{
// Write the uploaded file to the stream
Stream stream = reqftp. getrequeststream ();
// KB of each file stream read
Contenlen = FS. Read (buff, 0, bufflength );
// The stream content has not ended
While (contenlen! = 0)
{
Stream. Write (buff, 0, contenlen );
Contenlen = FS. Read (buff, 0, bufflength );
}
// Close two streams
Stream. Close ();
FS. Close ();
}
Catch (exception E)
{
MessageBox. Show ("Upload error", E. Message );
}
}
/// <Summary>
/// Download an object
/// </Summary>
/// <Param name = "filepath"> </param>
/// <Param name = "FILENAME"> </param>
/// <Param name = "errorinfo"> </param>
/// <Returns> </returns>
Public bool downglad (string filepath, string filename)
{
Try
{
String onlyfilename = path. getfilename (filename );
String newfilename = filepath + "//" + onlyfilename;
If (file. exists (newfilename ))
{
// Errorinfo = string. Format ("the local file {0} already exists and cannot be downloaded", newfilename );
Return false;
}
String url = "ftp: //" + ftpserverip + "/" + filename;
// Get the connection
Connect (URL );
Reqftp. Credentials = new networkcredential (ftpuserid, ftppassword );
Ftpwebresponse response = (ftpwebresponse) reqftp. getresponse ();
Stream ftpstream = response. getresponsestream ();
Long Cl = response. contentlength;
Int buffer size = 2048;
Int readcount;
Byte [] buffer = new byte [buffersize];
Readcount = ftpstream. Read (buffer, 0, buffersize );
Filestream outputstream = new filestream (newfilename, filemode. Create );
While (readcount> 0)
{
Outputstream. Write (buffer, 0, readcount );
Readcount = ftpstream. Read (buffer, 0, buffersize );
}
Ftpstream. Close ();
Outputstream. Close ();
Response. Close ();
// Errorinfo = "";
Return true;
}
Catch (exception ex)
{
// Errorinfo = string. Format ("unable to download because {0}", Ex. Message );
MessageBox. Show ("Download error", Ex. Message );
Return false;
}
}