2

I wonder how do I check how much of a file has been uploaded/downloaded? I am using HttpWebRequest

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • Then are you *not* also handling the buffering yourself when writing/reading to the input/output streams of `HttpWebRequest`? By buffering, you have a natural iteration with which to increment progress. – Kirk Woll Oct 13 '10 at 14:19
  • I maybe wrong but all that buffering will be progress on the client side only right? not data I actually send over. or do you have some code or links for me to see what you mean? btw my current code looks something like http://codepad.org/gK16e0NU – Jiew Meng Oct 14 '10 at 10:00
  • possible duplicate of [Silverlight 4 RC File Upload with Upload Progress: how to?](http://stackoverflow.com/questions/2529558/silverlight-4-rc-file-upload-with-upload-progress-how-to) –  Jul 22 '12 at 10:36
  • See **[http://stackoverflow.com/a/2604279/240845](http://stackoverflow.com/a/2604279/240845)** for code. – mheyman Apr 04 '12 at 13:36

3 Answers3

3

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.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
-1

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.

norekhov
  • 3,915
  • 25
  • 45
  • Actually, the client buffers the data even when you set SendChunked. I tried this with an 18M file and it all sent in a few seconds, then the actual transmission took minutes. – Charles Sep 13 '14 at 19:26
-2

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.

RameshVel
  • 64,778
  • 30
  • 169
  • 213
  • 1
    Thank you but can you elaborate abit on how you would do this. – Jiew Meng Oct 14 '10 at 12:19
  • 2
    The callback is for when the request is done. there's no progress via the callback (other than 100%--which is likely *not* why the OP asked the question). that might explain the downvote – Peter Ritchie Mar 22 '13 at 20:27