I'm trying to copy a file from my local computer to an FTP server using C#. When I use the code below, the file is fully copied to the FTP server but **the original lines are cut into pieces of only 512 bytes long while they should be 1152,1126 or 1024 bytes long. ** The example file I used has now 16 lines instead of 7.
public void uploadLOTFILE(string username, string password)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.mine/mypathandfilename");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = true;
System.IO.Stream rs = request.GetRequestStream();
var lines = File.ReadLines(@"myLocalFile.txt");
foreach (var line in lines)
{
byte[] buffer = Encoding.ASCII.GetBytes(line);
Console.WriteLine("buffer.length:" + buffer.Length.ToString());
rs.Write(buffer,0,buffer.Length);
}
rs.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
Output from the Console.writeline for an example file:
buffer.length:1152
buffer.length:1126
buffer.length:1152
buffer.length:1152
buffer.length:1152
buffer.length:1152
buffer.length:1024
I've also used the exact copy from msdn (https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx) but this had the same result.
Edit: Also tried the following code: string filePath = @"myFilePath"; var fileName = Path.GetFileName(filePath); var request = (FtpWebRequest)WebRequest.Create("ftp://myftp");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
using (var fileStream = File.OpenRead(filePath))
{
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
requestStream.Close();
}
}
It gives the same result. The file is fully copied but every 512 bytes a newline is added.
With FileZilla I can do a correct FTP transfer of the same type of files.
I solved my problem using the build-in windows FTP that is accessible using the cmd.