C # FTP upload

Source: Internet
Author: User

 

Code
///   <Summary>
/// FTP upload function
///   </Summary>
///   <Param name = "ftpserverip"> </param>
///   <Param name = "FILENAME"> </param>
///   <Param name = "ftpuserid"> </param>
///   <Param name = "ftppassword"> </param>
Public   Static   Void Upload ( String Ftpserverip, String Filename, String Ftpuserid, String Ftppassword)
{
Fileinfo fileinf =   New Fileinfo (filename );

string URI = " ftp: // " + ftpserverip + " / " + fileinf. name;
ftpwebrequest reqftp;

// Create an ftpwebrequest object based on Uri
Reqftp = (Ftpwebrequest) ftpwebrequest. Create ( New Uri ( " FTP :// "   + Ftpserverip +   " / "   + Fileinf. Name ));

// FTP username and password
reqftp. credentials = New networkcredential (ftpuserid, ftppassword);

// the default value is true, the connection will not be closed
/// executed after a command
reqftp. keepalive = false ;

//Specify the command to be executed
Reqftp. Method=Webrequestmethods. FTP. uploadfile;

//Data transmission type
Reqftp. usebinary= True;

// Notification Server File size when uploading files
reqftp. contentlength = fileinf. length;

// set the buffer size to 2 kb
int bufflength = 2048 ;

Byte[] Buff= New Byte[Bufflength];
IntContentlen;

// Open a file stream (system. Io. filestream) to read the uploaded file.
Filestream FS = Fileinf. openread ();
Try
{
// Write uploaded files to the stream
Stream STRM = Reqftp. getrequeststream ();

//2 kb of each file stream read
Contentlen=FS. Read (buff,0, Bufflength );

// The stream content has not ended
While (Contentlen ! =   0 )
{
// Write content from file stream to upload stream
STRM. Write (buff, 0 , Contentlen );

Contentlen=FS. Read (buff,0, Bufflength );
}

// Close two streams
STRM. Close ();
FS. Close ();
}
Catch (Exception ex)
{
// MessageBox. Show (ex. message, "Upload error ");
Httpcontext. Current. response. Write ( " Upload error: "   + Ex. Message );
}
}

 

 

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.