The code below does not succeed, I copied someones code for basic FTP
upload then attempted to add code to handle large files. If a file is
above a certain size it is supposed to upload the file in chunks of
data, the progress bar never moves and eventually the program errors
out. I am new to c# but I thought my structure was good. Any
suggestions would be appreciated.
{
//Create FTP request
FtpWebRequest request = (FtpWebRequest)
FtpWebRequest.Create
(FTPAddress + "/" + Path.GetFileName(filePath));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username,
password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Load the file
FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
//Upload file
Stream reqStream = request.GetRequestStream();
MessageBox.Show(buffer.Length.ToString());
if (buffer.Length < 25000)
{
MessageBox.Show("uploading File all at once");
reqStream.Write(buffer, 0, buffer.Length);
}
else
{
MessageBox.Show("uploading File in pieces");
int percM;
percM = buffer.Length;
for (int i = 0; i <= buffer.Length; i += 2000)
{
int e;
int perc;
e = i + 1999;
perc = (i / percM) * 100;
reqStream.Write(buffer, i, e);
//MessageBox.Show(((i / buffer.Length) *
100).ToString());
progressBar1.Value = perc;
}
}
reqStream.Close();
progressBar1.Value = 100;
MessageBox.Show("Uploaded Successfully");
}