Skip to content

Instantly share code, notes, and snippets.

@paulodiogo
Last active June 11, 2022 20:34
Show Gist options
  • Save paulodiogo/9b51a5e3df50acf1af2ae5522a9041ac to your computer and use it in GitHub Desktop.
Save paulodiogo/9b51a5e3df50acf1af2ae5522a9041ac to your computer and use it in GitHub Desktop.
var response = await webDavClient.GetProcessedFile(uri, new GetFileParameters { CancellationToken = cancellationTokenSource.Token });
var size = response.ContentLength();
var destination = new MemoryStream();
byte[] buffer = new byte[bufferSize];
int read;
long totalRead = 0;
while ((read = await response.Stream.ReadAsync(buffer, 0, buffer.Length, cancellationTokenSource.Token)) != 0)
{
await destination.WriteAsync(buffer, 0, read);
totalRead += read;
Progress = totalRead / (double)size;
}
public static class WebDavStreamResponseExtensions
{
public static long? ContentLength(this WebDavStreamResponse message)
{
FieldInfo fields = typeof(WebDavStreamResponse).GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(x => x.FieldType == typeof(HttpResponseMessage));
HttpResponseMessage response = fields.GetValue(message) as HttpResponseMessage;
return response?.Content?.Headers?.ContentLength ?? 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment