简体   繁体   中英

How can I check the progress of a HttpWebRequest upload?

I wonder how 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM