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 따라서 while()은 작동하지 않습니다. 5Percent 변수만큼만 읽도록 지정 했더라도. 첫 번째 시도에서 전체 스트림을 읽는 것처럼 보입니다.청크를 올바르게 읽도록하려면 어떻게해야합니까?
감사합니다,
안드레이
WebClient.DownloadProgressChanged 이벤트에 등록한 다음 WebClient.DownloadDataAsync()를 호출하십시오. 이벤트 콜백에서 진행 상황을 업데이트 할 수 있습니다. – Jon