Use ftp to upload and download files-C #

Source: Internet
Author: User
Tags ftp file server memory

// FTP File Upload, filename: file to be uploaded, filenewname: name to be saved on the server
Private void upload (string filename, string filenewname)
{
String strfilename = "";

// Obtain information about the object
Fileinfo fileinf = new fileinfo (filename );
If (filenewname = "")
{
Strfilename = fileinf. Name;
}
Else
{
Strfilename = filenewname;
}

// Create an ftpwebrequest object based on the URI
Ftpwebrequest reqftp;
Reqftp = (ftpwebrequest) ftpwebrequest. Create (New uri ("ftp: // 127.0.0.1/" + strfilename ));

// FTP user name and password
Reqftp. Credentials = new networkcredential ("yans1", "123456 ");

// Destroy the connection to the server
Reqftp. keepalive = true;
// Obtain or set the command to be sent to the FTP server.
Reqftp. method = webrequestmethods. FTP. uploadfile;
// The data format of the transmitted file is binary.
Reqftp. usebinary = true;
// File Size
Reqftp. contentlength = fileinf. length;

// Set the buffer size to 2 kb.
Int bufflength = 2048;
Byte [] buff = new byte [bufflength];
Int contentlen;

Try
{
Stream STRM = reqftp. getrequeststream ();

// Read the file into the stream
Filestream FS = fileinf. openread ();
// Stores the data sent from the current request to the server.
// Package the file stream into a small byte array to prevent occupying too much Server Memory
Contentlen = FS. Read (buff, 0, bufflength );
// Cyclically write the file stream into the request stream to be sent to the FTP server
While (contentlen! = 0)
{
STRM. Write (buff, 0, contentlen );
Contentlen = FS. Read (buff, 0, bufflength );
}

FS. Close ();
STRM. Close ();
}
Catch (exception E)
{
Response. Write (E. Message. tostring ());
}
}

// FTP File Download, filepath: local path, filename: name of the file to be downloaded
Private void download (string filepath, string filename)
{
// Declare the request
Ftpwebrequest reqftp;
Reqftp = (ftpwebrequest) ftpwebrequest. Create (New uri ("ftp: // 127.0.0.1/" + filename ));

// Destroy the connection to the server
Reqftp. keepalive = true;
// Set the command form to be sent
Reqftp. method = webrequestmethods. FTP. downloadfile;
// Set the file to be passed in binary format
Reqftp. usebinary = true;
// Set the login user name and password
Reqftp. Credentials = new networkcredential ("yans1", "123456 ");
// Try
//{
// Obtain the FTP Response
Ftpwebresponse response = (ftpwebresponse) reqftp. getresponse ();
Stream ftpstream = response. getresponsestream ();

// Create a local save file stream
Filestream outputstream = new filestream (filepath + "/" + filename, filemode. Create );

Int buffer size = 2048;
Int readcount;
Byte [] buffer = new byte [buffersize];

readcount = ftpstream. read (buffer, 0, buffersize);
while (readcount> 0)
{< br> outputstream. write (buffer, 0, readcount);
readcount = ftpstream. read (buffer, 0, buffersize);
}

Outputstream. Close ();
Ftpstream. Close ();
Response. Close ();
//}
// Catch (exception E)
//{
// Response. Write (E. Message. tostring ());
//}

}

// Obtain the list of all files in the FTP directory
Private string [] getfilelist ()
{
Stringbuilder result = new stringbuilder ();

// Create a request
Ftpwebrequest reqftp;
Reqftp = (ftpwebrequest) ftpwebrequest. Create (New uri ("ftp: // 127.0.0.1" + "/"));

// Set Request attributes
Reqftp. keepalive = true;
Reqftp. method = webrequestmethods. FTP. listdirectory;
Reqftp. usebinary = true;

// Set the user and password
Reqftp. Credentials = new networkcredential ("yans1", "123456 ");
Ftpwebresponse resftp = (ftpwebresponse) reqftp. getresponse ();

// obtain the response stream
streamreader READ = new streamreader (resftp. getresponsestream ();

string line = read. Readline ();
while (line! = NULL & line! = "")
{// determines whether the file is a file or a file
result. append (line);
result. append (char) 1);
line = read. readline ();
}< br> // remove the last character
result. remove (result. tostring (). lastindexof (char) 1), 1);
Read. close ();
resftp. close ();
return result. tostring (). split (char) 1);
}

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.