Skip to content

Commit a32235d

Browse files
committed
增加文件上传进度事件
1 parent 585b4ae commit a32235d

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

WebApiClient/Contexts/ApiActionContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ private async Task ExecRequestAsync()
164164
{
165165
try
166166
{
167+
var completionOption = this.ApiActionDescriptor.Return.DataType.IsHttpResponseWrapper ?
168+
HttpCompletionOption.ResponseHeadersRead :
169+
HttpCompletionOption.ResponseContentRead;
170+
167171
this.ResponseMessage = await this.HttpApiConfig.HttpClient
168-
.SendAsync(this.RequestMessage, cancellation.Token)
172+
.SendAsync(this.RequestMessage, completionOption, cancellation.Token)
169173
.ConfigureAwait(false);
170174

171175
this.Result = await this.ApiActionDescriptor.Return.Attribute
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
3+
namespace WebApiClient
4+
{
5+
/// <summary>
6+
/// 表示下载进度
7+
/// </summary>
8+
public class DownloadProgressEventArgs : EventArgs
9+
{
10+
/// <summary>
11+
/// 获取当前收到的字节数
12+
/// </summary>
13+
public long CurrentBytes { get; }
14+
15+
/// <summary>
16+
/// 获取总字节数
17+
/// </summary>
18+
public long? TotalBytes { get; }
19+
20+
/// <summary>
21+
/// 获取是否已完成
22+
/// </summary>
23+
public bool IsCompleted { get; }
24+
25+
/// <summary>
26+
/// 获取当前进度
27+
/// </summary>
28+
public double Progress
29+
{
30+
get
31+
{
32+
if (this.TotalBytes == null)
33+
{
34+
return 0.00d;
35+
}
36+
return (double)this.CurrentBytes / (double)this.TotalBytes.Value;
37+
}
38+
}
39+
40+
/// <summary>
41+
/// 下载进度
42+
/// </summary>
43+
/// <param name="current">当前收到的字节数</param>
44+
/// <param name="total">总字节数</param>
45+
/// <param name="isCompleted">是否已完成</param>
46+
public DownloadProgressEventArgs(long current, long? total, bool isCompleted)
47+
{
48+
this.CurrentBytes = current;
49+
this.TotalBytes = total;
50+
this.IsCompleted = isCompleted;
51+
}
52+
53+
/// <summary>
54+
/// 转换为字符串
55+
/// </summary>
56+
/// <returns></returns>
57+
public override string ToString()
58+
{
59+
return $"{Math.Round(this.Progress * 100, 2)}%";
60+
}
61+
}
62+
}

WebApiClient/HttpResponseFile.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ namespace WebApiClient
1313
[DebuggerDisplay("FileName = {FileName}")]
1414
public class HttpResponseFile : HttpResponseWrapper
1515
{
16+
/// <summary>
17+
/// 下载进度变化事件
18+
/// </summary>
19+
public event EventHandler<DownloadProgressEventArgs> DownloadProgressChanged;
20+
1621
/// <summary>
1722
/// 获取响应的友好文件名称
1823
/// </summary>
@@ -86,13 +91,25 @@ public async Task SaveAsAsync(Stream stream)
8691
}
8792

8893
var length = 0;
94+
var current = 0L;
8995
var buffer = new byte[8 * 1024];
9096
var sourceStream = await this.HttpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
9197

98+
var args = new DownloadProgressEventArgs(current, this.FileSize, false);
99+
this.DownloadProgressChanged?.Invoke(this, args);
100+
92101
while ((length = await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
93102
{
103+
current = current + length;
104+
105+
args = new DownloadProgressEventArgs(current, this.FileSize, false);
106+
this.DownloadProgressChanged?.Invoke(this, args);
107+
94108
await stream.WriteAsync(buffer, 0, length).ConfigureAwait(false);
95109
}
110+
111+
args = new DownloadProgressEventArgs(current, this.FileSize, true);
112+
this.DownloadProgressChanged?.Invoke(this, args);
96113
}
97114
}
98115
}

0 commit comments

Comments
 (0)