Reputation: 78
I'm trying to develop a little program which can support several uploads simultaneously. However, I'm facing a problem : if I have one file uploading, there is a progress bar; but when I put more files to upload, I don't know how to create progress bars for each file independently and how to automatically add additional bars on multiple uploads.
I use WebClient
class with UploadFileAsync
.
String file
is the path needed for uploading and uploadForm
is the Form
where I would show progress bars and do uploads.
EDIT : final code to make it functional
private void UploadEnProgres(object sender, UploadProgressChangedEventArgs e, String file, ProgressBar nouvelleProgressBar)
{
Console.WriteLine("{0} : {1} octet sur {2} au total. {3} % envoyés...", file, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage);
nouvelleProgressBar.Value = (int)((e.BytesSent * 100) / e.TotalBytesToSend);
}
private void UploadFini(object sender, UploadFileCompletedEventArgs e, String file, ProgressBar nouvelleProgressBar, TextBox nouvelleTextBox, Int32 hauteur)
{
if ((e.Cancelled) || (e.Error != null))
{
Console.WriteLine("{0} : ERREUR -- {1}", file, e.Error);
return;
}
Console.WriteLine("{0} : upload terminé, statut : ", file, System.Text.Encoding.UTF8.GetString(e.Result));
uploadForm.Controls.Remove(nouvelleProgressBar);
uploadForm.Controls.Remove(nouvelleTextBox);
hauteur = 12;
}
public void HTTPClientUpload(String file)
{
Console.WriteLine("Upload button.");
WebClient clientUpload = new WebClient();
String authInfo;
authInfo = utilisateur + ":" + motDePasse;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
clientUpload.Headers["Authorization"] = "Basic " + authInfo;
// CODE HTTP UPLOAD
this.hauteur += 22;
ProgressBar nouvelleProgressBar = new ProgressBar();
nouvelleProgressBar.Size = new Size(180, 20);
nouvelleProgressBar.Maximum = 100;
nouvelleProgressBar.Minimum = 0;
nouvelleProgressBar.Location = new Point(258, hauteur);
nouvelleProgressBar.Visible = true;
uploadForm.Controls.Add(nouvelleProgressBar);
TextBox nouvelleTextBox = new TextBox();
nouvelleTextBox.Size = new Size(180, 20);
nouvelleTextBox.Location = new Point(70, hauteur);
nouvelleTextBox.Text = Path.GetFileName(file);
nouvelleTextBox.Enabled = false;
uploadForm.Controls.Add(nouvelleTextBox);
clientUpload.Headers.Add("Chemin", "/" + identifiantAppareil + file.Replace(@"\", "/"));
clientUpload.UploadFileCompleted += new UploadFileCompletedEventHandler((sender, e) => UploadFini(sender, e, Path.GetFileName(file), nouvelleProgressBar, nouvelleTextBox, hauteur));
clientUpload.UploadProgressChanged += new UploadProgressChangedEventHandler((sender, e) => UploadEnProgres(sender, e, Path.GetFileName(file), nouvelleProgressBar));
clientUpload.UploadFileAsync(new Uri(urlServeur, "v1/ul"), file);
}
Upvotes: 3
Views: 6058
Reputation: 2496
For organization, I would suggest a TableLayoutPanel to add newly created controls to.
Upvotes: 0
Reputation: 1753
//Create a new progressbar each time you start an upload in code
var progressbar = new ProgressBar()
//add it to the form
uploadForm.Controls.Add(progressbar );
you'll need to look into how you group / position them
Upvotes: 3