简体   繁体   中英

Most efficient/accurate way to download/upload with progress and speed in C#?

Uploading and downloading a file is easy enough. Doing so while displaying progress and a transfer rate has me questioning the best way to do it. I'm looking for that one perfect way. The method that's easy to use, accurate, and light weight. Tried and true.

The situation I'm talking about is, for example, downloading a file from a WebResponse or some other stream based transfer. Downloading it is as simple as (4.0 here):

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream bufferedresponse = new BufferedStream(response.GetResponseStream());
bufferedresponse.CopyTo(TargetStream);
bufferedresponse.Close();

You can't find a transfer rate until after it's done downloading, and even that is just an average. I'm thinking a touch more instantaneous. I don't always need an entire WebBrowser object, especially in REST API wrappers (where this particular sample is from), besides, doesn't WebBrowser also attempt to parse DOM and render it's content?

Are there binding techniques or simple multi-threaded approaches to this? Are there attributes/events of a stream class I'm overlooking? Another class entirely?

You need to create an "ObservableStream" that will fire subscribable events that you can handle by incrementing a progress bar and updating telemetry.

Try this implementation, using the Reactive Extensions library: http://staceyw.posterous.com/observe-a-network-stream-using-rx

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