How can I check the progress of a HttpWebRequest upload?
I wonder ho开发者_运维技巧w do I check how much of a file has been uploaded/downloaded? I am using HttpWebRequest
You can do this is you use async mode on the HttpWebRequest
- there is a working sample (based on the MSDN doc sample code) here. Brief description:
Here’s a little Win Forms client that allows you to download a single file from a server, using either HTTP or FTP. It shows download progress and displays the average transfer rate, in kb/sec. It also demonstrates how to use the HttpWebRequest and FtpWebRequest classes in System.Net to do file downloads.
As long as you set either HttpWebRequest.ContentLength or HttpWebRequest.SendChunked before calling GetRequestStream, the data you send will be sent to the server with each call to Stream.[Begin]Write. If you write the file in small chunks suggests, you can get an idea of how far along you.
You have to call it asynchronously to update the progress of your upload/download.
HttpWebRequest have methods like
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state);
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
accepting asynchronous callbacks.
精彩评论