Reputation: 19
I have this code and I would like to add a progress bar. Please give me some instruction. I don't know where to start the coding. Thanks!
private void uploadbutton_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.ShowDialog();
FileInfo feltoltfile = new FileInfo(openFileDialog1.FileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpadress + "/" + feltoltfile.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(textBox1.Text, textBox2.Text);
StreamReader sourceStream = new StreamReader(feltoltfile.ToString());
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
serverstatus.Items.Add(response.StatusDescription +" "+feltoltfile.Name+ " --> " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second);
response.Close();
ftplista.Items.Clear();
FTPlistalekerdezes(ftpadress, textBox1.Text, textBox2.Text);
MessageBox.Show("Ready!");
}
catch(Exception ex)
{
MessageBox.Show("Error!" + ex.Message);
}
}
Upvotes: 0
Views: 919
Reputation: 12766
Untested code, but should give you an idea. Further more I used the using statement:
openFileDialog1.ShowDialog();
FileInfo feltoltfile = new FileInfo(openFileDialog1.FileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpadress + "/" + feltoltfile.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(textBox1.Text, textBox2.Text);
using (var sourceStream = feltoltfile.OpenRead())
using (var requestStream = request.GetRequestStream())
{
long fileSize = request.ContentLength = feltoltfile.Length;
long bytesTransfered = 0;
byte[] buffer = new byte[4096];
int read;
while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0) //while there are still bytes to be copied
{
requestStream.Write(buffer, 0, read);
requestStream.Flush();
bytesTransfered += read;
//trigger progress event...
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
serverstatus.Items.Add(response.StatusDescription + " " + feltoltfile.Name + " --> " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second);
}
ftplista.Items.Clear();
FTPlistalekerdezes(ftpadress, textBox1.Text, textBox2.Text);
MessageBox.Show("Ready!");
Upvotes: 1