WebDavClient
WebDavClient copied to clipboard
Progress monitoring with GetRawFile()
Hi, I am trying to use this nice client to download a large data file (>2GB) and would like to monitor the progress and download speed.
Is there any build-in API for doing so? Otherwise I guess I will need to read the stream returned by GetRawFile(), something like
using (var response = await client.GetRawFile("data.mp4"))
{
using (fileStream = new FileStream("data.mp4", FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
{
var totalRead = 0L;
var totalReads = 0L;
var buffer = new byte[8192];
var isMoreToRead = true;
do
{
var read = await response.Stream.ReadAsync(buffer, 0, buffer.Length);
if (read == 0)
{
isMoreToRead = false;
}
else
{
await fileStream.WriteAsync(buffer, 0, read);
totalRead += read;
totalReads += 1;
if (totalReads % 2000 == 0)
{
Console.WriteLine(string.Format("total bytes downloaded so far: {0:n0}", totalRead));
}
}
}
while (isMoreToRead);
}
}
Your help is much appreciated.
Hi Ning Ma,
Unfortunately, there's no built-in API to monitor download progress and there's no plan to add it to the lib.
You either need to read a stream yourself the way you proposed or pass your own instance of HttpClient
with ProgressMessageHandler
that supports reporting the progress (see https://github.com/dotnet/corefx/issues/6849). But I'm not sure if ProgressMessageHandler
is a part of ASP.NET Core though.
Anyway, thank you for asking this question and posting a code snippet. This can be helpful to other users.
I submitted pull request #52 specifically to support progress monitoring. It also lowers memory consumption since the entire file isn't read into memory.
I separately use a wrapper stream that then reports to an IProgress<double>
interface. See this gist for an example.
A version 2.5.0 with merged #52 is available in NuGet.