File upload to FTP server

9:39:00 AM 0 Comments A+ a-

Here is C# example source code to show you how to upload file to a FTP server.

You will need fill in your ftp server, username and password data.

Code:
FileInfo objFile = new FileInfo("c:\filetuupload.txt");
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://uploadftpserver.com/" + objFile.Name));
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.Credentials = new NetworkCredential("username", "password");
ftpWebRequest.UseBinary = true;
ftpWebRequest.KeepAlive = false;
ftpWebRequest.ContentLength = objFile.Length;
ftpWebRequest.ServicePoint.ConnectionLimit = 1;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.UsePassive = true;
 
int intBufferLength = 8 * 512;
byte[] objBuffer = new byte[intBufferLength];
 
FileStream objFileStream = objFile.OpenRead();
Stream objStream = ftpWebRequest.GetRequestStream();
 
int len = 0;
while ((len = objFileStream.Read(objBuffer, 0, intBufferLength)) != 0)
{
 objStream.Write(objBuffer, 0, len);
}
objStream.Flush();
objStream.Close();
objFileStream.Close();