问题描述
我有一个文件下载功能:
i have a file downloader function:
HttpClientHandler aHandler = new HttpClientHandler();
aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
HttpClient aClient = new HttpClient(aHandler);
aClient.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await aClient.GetAsync(url);
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
// To save downloaded image to local storage
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
filename, CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
await writer.StoreAsync();
//current.image.SetSource(randomAccessStream);
writer.DetachStream();
await fs.FlushAsync();
我怎样才能实现进度条功能?
也许我可以得到迄今为止写的作家字节?还是什么?
How can i realize progress bar functionality?Maybe i can get the writers bytes written so far? Or something?
P.S。我不能使用DownloadOperation(背景传输),因为从服务器请求证书数据 - 而这个功能不存在DownloadOperations
推荐答案
去使用最好的办法 Windows.Web.Http.HttpClient
而不是 System.Net.Http.HttpClient
。第一个支持的进展。
The best way to go is using Windows.Web.Http.HttpClient
instead of System.Net.Http.HttpClient
. The first one supports progress.
但是,如果由于某种原因,你要坚持到System.Net之一,你将需要实现自己的进步。
But if for some reason you want to stick to the System.Net one, you will need to implement your own progress.
删除 DataWriter
,删除 InMemoryRandomAccessStream
和添加 HttpCompletionOption.ResponseHeadersRead
到 GetAsync
呼叫所以它受到了全响应时不只要接收到标头返回。即:
Remove the DataWriter
, remove the InMemoryRandomAccessStream
and add HttpCompletionOption.ResponseHeadersRead
to GetAsync
call so it returns as soon as headers are received, not when the whole response is received. I.e.:
// Your original code.
HttpClientHandler aHandler = new HttpClientHandler();
aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
HttpClient aClient = new HttpClient(aHandler);
aClient.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await aClient.GetAsync(
url,
HttpCompletionOption.ResponseHeadersRead); // Important! ResponseHeadersRead.
// To save downloaded image to local storage
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
filename,
CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
// New code.
Stream stream = await response.Content.ReadAsStreamAsync();
IInputStream inputStream = stream.AsInputStream();
ulong totalBytesRead = 0;
while (true)
{
// Read from the web.
IBuffer buffer = new Windows.Storage.Streams.Buffer(1024);
buffer = await inputStream.ReadAsync(
buffer,
buffer.Capacity,
InputStreamOptions.None);
if (buffer.Length == 0)
{
// There is nothing else to read.
break;
}
// Report progress.
totalBytesRead += buffer.Length;
System.Diagnostics.Debug.WriteLine("Bytes read: {0}", totalBytesRead);
// Write to file.
await fs.WriteAsync(buffer);
}
inputStream.Dispose();
fs.Dispose();
这篇关于与HttpClient的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!