Main Site Documentation

Bytes lost during FTP upload from Cobra II NET


#1

Can anyone shed some light on a problem Im having uploading data to an FTP server? When I run this code on a file that is 2,749,710 bytes, I randomly get 2,749,702 to 2,749,706 bytes sent to the server. The file is being read from an SD card on a Cobra II NET. When I transmit a buffer array without filling it from a FileStream the correct number of bytes are transmitted every time. I can manually upload the file with FileZilla and the correct number of bytes are received. I can also copy from the source file to another file on the SD card using the same processor and the correct number of bytes are copied. I assumed it was the last few bytes that may be stuck in a buffer somewhere but the losses seem to occur randomly in the transmission. Slowing the transmission down or reducing the buffer size doesnt seem to help. Any ideas? Really stuck on this one. Thanks!


FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://servername.com/dir1/dir2/file.lg");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user", "password");

const int BUFFERSZ = 2048;
byte[] buffer = new byte[BUFFERSZ];

FileStream sourceStream = new FileStream(@ "SD\file.lg", FileMode.Open, FileAccess.Read, FileShare.None, BUFFERSZ);

long length = sourceStream.Length;

Stream requestStream = request.GetRequestStream();
int bytesRead;
long totalBytes = 0;

while (totalBytes < length)
{
    bytesRead = sourceStream.Read(buffer, 0, BUFFERSZ);
    requestStream.Write(buffer, 0, bytesRead);
    totalBytes += bytesRead;
    Debug.Print("Bytes sent: " + totalBytes.ToString());
}
sourceStream.Close();

requestStream.Close();
requestStream.Dispose();
request.Dispose();

Debug.Print("Upload File Complete, status ");



#2

I believe the corruption comes as the result of the file being transferred in ASCII mode instead of binary. Does anyone know how to send the TYPE I command using the NETMF FTP library? Thanks.


#3

That is the problem! Seems the 4.2 FtpWebRequest does not support binary transfers. Thank you @ davidh for your FTP code. Works! #happyengineer!