How to upload file using C#

Welcome to InspiringWorm. Follow this step to upload the file using C#.

Create an FTP request

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + “/” + Path.GetFileName(filePath));

request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = false;
request.UseBinary = true;
request.KeepAlive = false;

Step 2:  load the file

FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];

stream.Read(buffer, 0, buffer.Length);
stream.Close();

Step 3: Upload the file

Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();

Leave a Comment