0

My question is similar to this one and this one, but I couldn't get either of them to work in my application. Perhaps I'm missing something?

Here is what I want to accomplish:

  • Upload a string from memory via FTP
  • Show and update a progress bar with a background worker

And here is the code I have now:

private void bkgd_Submit_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    string host = @"ftp://ftp.somesite.net/submissions/";
    string user = "username";
    string pass = "password";
    string[] lines = { "First line", "Second line", "Third line" };

    string ftpFile = host + "filename.txt";
    //the string that needs to get uploaded
    string message = "Hello this is the first order.";

    FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFile);
    ftpRequest.Credentials = new NetworkCredential(user, pass);
    ftpRequest.KeepAlive = false;
    ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
    ftpRequest.UseBinary = true;
    byte[] messageContent = Encoding.ASCII.GetBytes(message);
    ftpRequest.ContentLength = messageContent.Length;
    int buffLength = 2048;
    Stream ftpStream = ftpRequest.GetRequestStream();
    int total_bytes = (int)messageContent.Length;

    //this is the area I need the most help with
    while (total_bytes < messageContent.Length)
    {
        ftpStream.Write(messageContent, total_bytes, buffLength);
        total_bytes += buffLength;
        var progress = total_bytes * 100.0 / messageContent.Length;
        bkgd_Submit.ReportProgress((int)progress);
    }
    ftpStream.Close();
}

The program skips over the while block, because total_bytes is always equal to messageContent.Length. I don't know enough about FTP uploading to know what the buffering does and what I should be doing differently.

Thanks so much for your help!

Community
  • 1
  • 1
gnarlybracket
  • 1,691
  • 4
  • 18
  • 37

1 Answers1

0

You're setting total_bytes to be equal to messageContent.Length, then checking to see if it's less that that. How can it be less when you just set them to be equal?

int total_bytes = (int)messageContent.Length;

//this is the area I need the most help with
while (total_bytes < messageContent.Length)
Steve
  • 6,334
  • 4
  • 39
  • 67
  • I sure I must appear like a total noob :) but this (http://stackoverflow.com/questions/10479131/c-sharp-upload-a-byte-inside-an-ftp-server) answer is the reason I have it like that. I don't have a good grasp on FTP uploading, so if you could explain what the `while` loop should look like, I would appreciate it very much. – gnarlybracket Dec 09 '13 at 21:22
  • In the example you linked, `total_bytes` is being set to `messageContent.Length`. However, the condition in the example is `while (total_bytes > 0)`, not `while (total_bytes < messagecontent.Length)`. Basically, he's writing `x` number of bytes, then subtracting `x` from `total_bytes` until `total_bytes == 0`, which means there are no more bytes to send. I don't know how ftpStream works, but it looks like you might want to set `total_bytes` to zero at first, then add the number of bytes after each write, until `total_bytes` equals the length of the whole file. – Steve Dec 10 '13 at 15:00