C # ftp file folder operation upload/download

Source: Internet
Author: User
Tags ftp connection ftp file

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;
}
}

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.