C # ftp file upload class

Source: Internet
Author: User
// Call the ftpuploadfile ("192.168.1.60", "ZCY", "123", @ "C: \ 1.txt ");

# Region FTP upload file // <summary> // FTP upload file // </Summary> // <Param name = "ftpserverip"> FTP Server IP address </ param> /// <Param name = "ftpuserid"> FTP Logon account </param> /// <Param name = "ftppassword"> FTP logon password </param> // /<Param name = "FILENAME"> file name (absolute path) </param> Private Static void ftpuploadfile (string ftpserverip, string ftpuserid, string ftppassword, string filename) {// Upload File stream filestream uploadfilestream = NULL; // ft P stream ftpstream = NULL; try {// get the uploaded file var uploadfile = new fileinfo (filename); // create the ftpwebrequest object var ftprequest = (ftpwebrequest) ftpwebrequest. create (New uri ("ftp: //" + ftpserverip + "/" + uploadfile. name); // FTP login ftprequest. credentials = new networkcredential (ftpuserid, ftppassword); // The default value is true, and the connection will not be closed // ftprequest is executed after a command. keepalive = false; // FTP request execution method ftprequest. method = webrequest Methods. FTP. uploadfile; // specifies the data transmission type ftprequest. usebinary = true; // notifies the server of the file size ftprequest when uploading files. contentlength = uploadfile. length; // set the buffer size to 2 kb const int bufflength = 2048; var buff = new byte [bufflength]; // open a file stream and read the uploaded file uploadfilestream = uploadfile. openread (); // write the uploaded file to the stream ftpstream = ftprequest. getrequeststream (); // 2kb var contentlen = uploadfilestream of each file stream read. read (buff, 0, bufflength); // stream Content not ended while (contentlen! = 0) {// write content from file stream to upload stream ftpstream. write (buff, 0, contentlen); contentlen = uploadfilestream. read (buff, 0, bufflength) ;}} catch (exception ex) {console. writeline (ex. message);} finally {If (uploadfilestream! = NULL) {uploadfilestream. Close ();} If (ftpstream! = NULL) {ftpstream. Close () ;}}# endregion

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.