Я пытаюсь загрузить данные с помощью объекта Webclient порциями по 5% каждый.Причина в том, что мне нужно сообщать о прогрессе для каждого загруженного блока.
Вот код, который я написал для выполнения этой задачи:
private void ManageDownloadingByExtractingContentDisposition(WebClient client, Uri uri)
{
//Initialize the downloading stream
Stream str = client.OpenRead(uri.PathAndQuery);
WebHeaderCollection whc = client.ResponseHeaders;
string contentDisposition = whc["Content-Disposition"];
string contentLength = whc["Content-Length"];
string fileName = contentDisposition.Substring(contentDisposition.IndexOf("=") +1);
int totalLength = (Int32.Parse(contentLength));
int fivePercent = ((totalLength)/10)/2;
//buffer of 5% of stream
byte[] fivePercentBuffer = new byte[fivePercent];
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
int count;
//read chunks of 5% and write them to file
while((count = str.Read(fivePercentBuffer, 0, fivePercent)) > 0);
{
fs.Write(fivePercentBuffer, 0, count);
}
}
str.Close();
}
Проблема - когда он попадает в str.Read(), он приостанавливает столько же, сколько читает весь поток, а затем считает 0. Таким образом, while () не работает, даже если я указал чтение только столько, сколько переменная fivePercent.Похоже, что он читает весь поток с первой попытки.
Как мне сделать так, чтобы он правильно читал куски?
Спасибо,
Андрей