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