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?
CARVIEW |
Select Language
HTTP/2 301
date: Thu, 24 Jul 2025 23:28:32 GMT
content-length: 0
location: /questions/982299/getting-the-upload-progress-during-file-upload-using-webclient-uploadfile
cf-ray: 964731c6ae0e20c5-BLR
cf-cache-status: DYNAMIC
cache-control: private
set-cookie: prov=1067a67d-703e-476c-9e9c-31880f843dea; expires=Fri, 24 Jul 2026 23:28:31 GMT; domain=.stackoverflow.com; path=/; secure; httponly
strict-transport-security: max-age=31536000; includeSubDomains
content-security-policy: upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com
feature-policy: microphone 'none'; speaker 'none'
x-clacks-overhead: GNU Terry Pratchett
x-frame-options: SAMEORIGIN
x-request-guid: 4d2e909f-02af-45a7-87ac-ba05b41a71a8
x-worker-origin-response-time: 259000000
vary: Accept-Encoding
x-dns-prefetch-control: off
set-cookie: __cflb=02DiuFA7zZL3enAQJD3AX8ZzvyzLcaG7v3STprD71ro8G; SameSite=Lax; path=/; expires=Fri, 25-Jul-25 22:28:32 GMT; HttpOnly
set-cookie: prov=1067a67d-703e-476c-9e9c-31880f843dea; Path=/; HttpOnly; Domain=stackoverflow.com
set-cookie: __cf_bm=pITNIUAsHKLnXMrxRR930bXqJQyiX9P5ZtGytdw2eio-1753399712-1.0.1.1-vJmKhvxGMS22nvDVIY_QGpNjt2K4hgoPaWwNyv4iSwEN.ybCYZTNkp2u38fjtxSUUEjaGB6xFZ9iNuyazOU9oOYva6.XWV5M.FCSAOxvTGE; path=/; expires=Thu, 24-Jul-25 23:58:32 GMT; domain=.stackoverflow.com; HttpOnly; Secure; SameSite=None
set-cookie: _cfuvid=fSPQNnI.9pBchARPrC0ltb0Yc8PyODQuRrK5WoLVjc0-1753399712058-0.0.1.1-604800000; path=/; domain=.stackoverflow.com; HttpOnly; Secure; SameSite=None
server: cloudflare
HTTP/2 200
date: Thu, 24 Jul 2025 23:28:32 GMT
content-type: text/html; charset=utf-8
cf-ray: 964731c8680f20c5-BLR
cf-cache-status: DYNAMIC
cache-control: private
set-cookie: prov=1067a67d-703e-476c-9e9c-31880f843dea; expires=Fri, 24 Jul 2026 23:28:32 GMT; domain=.stackoverflow.com; path=/; secure; httponly
strict-transport-security: max-age=31536000; includeSubDomains
vary: Accept-Encoding
content-security-policy: upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com
feature-policy: microphone 'none'; speaker 'none'
x-clacks-overhead: GNU Terry Pratchett
x-frame-options: SAMEORIGIN
x-request-guid: 561fd8e6-c3f8-4921-b497-732077cdba99
x-worker-origin-response-time: 278000000
x-dns-prefetch-control: off
server: cloudflare
content-encoding: gzip
c# - Getting the upload progress during file upload using Webclient.Uploadfile - Stack Overflow
Skip to main content
Stack Overflow
5
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising Reach devs & technologists worldwide about your product, service or employer brand
- Knowledge Solutions Data licensing offering for businesses to build and improve AI tools and models
- Labs The future of collective knowledge sharing
- About the company Visit the blog
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
asked Jun 11, 2009 at 16:54
Add a comment
|
2 Answers 2
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
}
answered Jun 11, 2009 at 17:00
-
Thanks. I'm working with multithreading, the file upload is already running on a different thread. So should I just use the Uploadfile method or the UploadfileAsync method? Commented Jun 11, 2009 at 17:12
-
You'll still need to UploadFileAsync I'm afraid, the thread will block on a call to UploadFile so those events will never get called. You can recreate your own blocking by setting a bool flag when you start the upload, reset it in uploadcomplete, then thread.sleep until the flag is cleared. Commented Jun 11, 2009 at 17:17
-
note:
UploadProgressChanged
event does not work well withUploadDataAsync
method Commented Jul 3, 2017 at 16:18 -
-
Did you use any cancelAsyc in your method. if yes how did you use it in a way the cancel upload stop on the spot and not wait to end the upload process to cancel?– H_HCommented Dec 3, 2019 at 9:58
Add your event handler to WebClient.UploadProgressChanged and call WebClient.UploadFileAsync.
See the WebClient.UploadProgressChanged documentation for an example.
- The Overflow Blog
-
-
- Featured on Meta
-
-
-
-
-
Linked
Related
Hot Network Questions
- In this video why are the astronauts wearing only their flight suits during dragon training while in others they are in their full starman suits?
- Does Philosophy Need Quotes?
- Why is this delayed differential system giving errors in plotting the solution?
- Are birth control pills classified as a carcinogenic at the same level as asbestos and tobacco?
- How to move points in geometry nodes like a venetian blind?
- Do things unknown automatically become unknowable in normal epistemic modal logic
- How to \input file as arguments?
- Did Germany declare the Donetsk People’s Republic to be a terrorist organization, at some point?
- Early Sailing days in September 1066... Could Harold defeat William at Hastings, if it happened before Stamford Bridge?
- Processing All Files [0-9]*tex
- Can authors be blacklisted by academic publishers for multiple rejections without any ethical misconduct?
- Dispute over reuse of shared research data
- Must hospitals care for unidentifiable individuals indefinitely?
- Trinity question: what does to subsist/subsistence mean?
- Huge Difference in Interaction P-values Between Linear vs. Ordinal Regression (0.991 vs. 0.001)
- What is the DC voltage during regenerative braking in a battery–inverter–induction motor system?
- Why does my laptop's display turn off when I'm away from the keyboard?
- spots / stains on tubeless tire
- Using a MCU and PCB to actuate a 24V solenoid
- Why is `&self` allowed in the parameters of `&mut self` methods, but not `&mut self`?
- List Based Off Another List
- How much time should I take for "self transfer" without checked-in baggage?
- Why is this endgame winning for Black?
- Why does “brew search” for “peertube” match with “freetube”? How does the search work?
default