C # uploading files using FtpWebRequest

Source: Internet
Author: User

The FtpWebRequest class under the System.Net namespace implements the. NET implementation of the FTP protocol.

    • FTPWEBREQUEST.KEEPALIVE Specifies whether the server should close the connection immediately after the request has completed
    • Ftpwebrequest.usebinary specifies that files are transferred in binary mode
    • Ftpwebrequest.method to set FTP commands
    • WebRequestMethods.Ftp.UploadFile is a command to upload a file

When uploading a file using the FtpWebRequest object, you need to write data to the stream returned by the GetRequestStream method.

You need to refer to the following namespaces

UsingSystem.  Net;  UsingSystem.  IO;        

FTP Upload file Code implementation:

Public voidFtpfile(StringFtpfilepath, StringInputfilepath) { StringFtphost= "127.0.0.1"; Here correct hostname or IP of the FTP server to be given StringFtpfullpath= "ftp://" +Ftphost+Ftpfilepath; FtpWebRequestFtp= (FtpWebRequest)FtpWebRequest.Create(Ftpfullpath);Ftp.Credentials = New NetworkCredential("UserID", "Password"); UserID and password for the FTP server to givenFtp.KeepAlive = True;Ftp.Usebinary = True;Ftp.Method = WebRequestMethods.Ftp.UploadFile; FileStreamFs= File.OpenRead(Inputfilepath); Byte[]Buffer= New Byte[Fs.Length];Fs.Read(Buffer, 0,Bufferlength);  Fs. Closestream FtpStream = Ftpgetrequeststream ();  Ftpstream. Write (buffer, 0, Buffer. Length Ftpstream. Close               /span>                

Call Method:

Ftpfile(@"/testfolder/testfile.xml",@"C:\testfile.xml");     

C # uploading files using FtpWebRequest

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.