我需要在应用程序内提供任意文件的下载进度。 我以为我可以使用HttpClient.GetInputStreamAsync进度,但它不是很好。进度通知非常罕见。下面的代码:Windows Store应用程序HttpClient进度
var httpClient = new HttpClient();
var path = new Uri(@"http://<path>");
using (var inputStream = (await httpClient.GetInputStreamAsync(path).AsTask(new MyProgress())).AsStreamForRead())
{
var outputFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("output.mov", CreationCollisionOption.ReplaceExisting);
using (var outputStream = (await outputFile.OpenAsync(FileAccessMode.ReadWrite)).AsStreamForWrite())
{
await inputStream.CopyToAsync(outputStream);
await outputStream.FlushAsync();
}
}
Debug.WriteLine("done");
private class MyProgress : IProgress<HttpProgress>
{
public void Report(HttpProgress value)
{
Debug.WriteLine("HTTP {0} progress {1}/{2}", value.Stage, value.BytesReceived, value.TotalBytesToReceive);
}
}
输出:
HTTP ResolvingName progress 0/
HTTP ConnectingToServer progress 0/
HTTP SendingHeaders progress 0/
HTTP WaitingForResponse progress 0/
HTTP ReceivingHeaders progress 0/
HTTP ReceivingContent progress 0/109192147
done
所以在时间接收的总字节数是已知的一些点,但没有文件下载过程中报告进度。
它做报告的进展,但同时也加载的所有内容到内存是我想避免什么。对不起,迟到了,很忙。 – aguyngueran