Webclient.openread Data de descarga en fragmentos
Pregunta
Estoy tratando de descargar datos utilizando un objeto WebClient en fragmentos de 5% cada uno. La razón es que necesito informar el progreso para cada fragmento descargado.
Aquí está el código que escribí para hacer esta tarea:
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();
}
El problema: cuando se llega a str.read (), se detiene tanto como leer toda la transmisión, y luego contar es 0. por lo que el while () no funciona, incluso si especificé leer solo tanto como el Variable de cinco por ciento. Parece que lee toda la transmisión en el primer intento.
¿Cómo puedo hacerlo para que lea los trozos correctamente?
Gracias,
Andrei
No hay solución correcta
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow