Following is the code snippet to copy txt file from a location to a ftp path:
WebRequest WRequest = WebRequest.Create(FtpPath + OriginalfileName);
WRequest.Method = WebRequestMethods.Ftp.UploadFile;
WRequest.Credentials = new NetworkCredential("myusername", "FtpPassword");
FileStream stream = File.OpenRead(OriginalFilePath);
byte[] buffer = new byte[stream.Length];
Stream RStream = WRequest.GetRequestStream();
RStream.Write(buffer, 0, buffer.Length);
RStream.Close();
But the copied file at ftp destination is always empty. Why?
You’re not populating the buffer with the contents of the file. You’re just setting the length in the following line of code:
And you’re not filling it with the contents of the file anywhere else, either, so you’re just sending empty data to the FTP server. The bytes may have a certain length, but it’s all empty bytes.