ryudice
ryudice

Reputation: 37366

How to download a file in pieces with many threads

I need to download a file from a website using different threads and downloading different sections of the file at once, I know I can use Webclient.downloadfile method but it doesnt support downloading a file in chunks. If you could point to a tutorial or give me an idea on how to do it I would appreciate it. Thanks!

Upvotes: 4

Views: 1390

Answers (4)

Jay
Jay

Reputation: 57919

There are a lot of ifs here, but if you are downloading, say, a giant text file, you could actually split it into many files on the server, and return the addresses of each to the client (or use a filename convention and just report how many sections there are), and the client could in turn could spin up the threads to download each of sections, which it could then reconstitute into a single, large file.

I'm not sure of your use case, but this particular scenario may not be likely to make anything go faster, if that is the idea.

Upvotes: 0

grossvogel
grossvogel

Reputation: 6782

The WebClient object has a 'Headers' property, which should let you define a 'Range' header to ask for only a part of the file.

Upvotes: 0

user195488
user195488

Reputation:

To piggy back on Rex's answer, there is no fool-proof way to know. Some web servers will provide you with a content-length or some will return -1 for length. Annoying, I know..

Your best bet is to specify a fixed range and utilize some heuristics or analysis to determine estimated length of your chunks over time.

You'll also want to look at this similar SO question on Multipart Downloading in C#.

Upvotes: 0

Rex M
Rex M

Reputation: 144122

The server at the other end, the one providing the file, has to support downloading in chunks as well. It would need some way to specify which byte position in the file you want to start at, instead of starting at the first and sending until the client stops accepting them, or it reaches the end of the file.

Assuming the server does support that, they would provide some kind of documentation on how to utilize it and you would definitely find help here turning that into code.

Upvotes: 3

Related Questions