WebClientオブジェクトをそれぞれ5%のチャンクで使用してデータをダウンロードしようとしています。その理由は、ダウンロードした各チャンクについて進捗状況を報告する必要があるからです。ここでWebClient.OpenReadチャンク内のデータをダウンロード
私はこの作業を行うに書いたコードです:
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でありますですから、たとえ5Percent変数だけを読み込むように指定したとしても、while()は機能しません。最初の試行でストリーム全体を読み込むように見えます。
チャンクを正しく読み取れるようにするにはどうすればよいですか?
おかげで、
アンドレイ
WebClient.DownloadProgressChangedイベントに登録してから、WebClient.DownloadDataAsync()を呼び出します。イベントコールバックの進捗状況を更新できます。 – Jon