Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the upload progress during file upload using Webclient.Uploadfile

I have an app that uploads files to server using the webclient. I'd like to display a progressbar while the file upload is in progress. How would I go about achieving this?

like image 985
Bruce Adams Avatar asked Jun 11 '09 16:06

Bruce Adams


1 Answers

WebClient.UploadFileAsync will allow you to do this.

WebClient webClient = new WebClient();
webClient.UploadFileAsync(address, fileName);
webClient.UploadProgressChanged += WebClientUploadProgressChanged;

...

void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
        Console.WriteLine("Upload {0}% complete. ", e.ProgressPercentage);
}

Note that the thread won't block on Upload anymore, so I'd recommend using:

 webClient.UploadFileCompleted += WebClientUploadCompleted;

...

 void WebClientUploadCompleted(object sender, UploadFileCompletedEventArgs e)
 {
     // The upload is finished, clean up
 }
like image 186
Matt Brindley Avatar answered Sep 20 '22 09:09

Matt Brindley