WebClient UploadFileAsync进度报告中的奇怪行为

CrA*_*HeR 10 c# system.net webclient

我需要一些奇怪的WebClient.UploadFileAsync()行为的帮助.我正在将文件上传到远程HTTP服务器(nginx)usind POST方法.POST通过PHP脚本(地址引用)处理.

我有这个简单的代码

public void uploadFile(string filePath)
{
    webClient = new WebClient();
    webClient.Credentials = new NetworkCredential(Constant.HTTPUsername,Constant.HTTPPassword);
    webClient.Headers.Add("Test", TestKey);
    webClient.UploadProgressChanged += webClient_UploadProgressChanged;
    webClient.UploadFileCompleted += webClient_UploadFileCompleted;

    try
    {
        webClient.UploadFileAsync(new Uri(Address), "POST", filePath);
    }
    catch (Exception error)
    {
        throw new CustomException(error.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

在UploadProgressChanged中,我只需使用给定的ProgressPercentage更新progressBar.第一个问题是报告的进度百分比,任何文件大小为:

[17.38.14] Progress: 0 Bytes Sent: 175 / 269264
[17.38.14] Progress: 1 Bytes Sent: 8367 / 269264
[17.38.14] Progress: 3 Bytes Sent: 16559 / 269264
[17.38.14] Progress: 4 Bytes Sent: 24751 / 269264
[17.38.14] Progress: 6 Bytes Sent: 32943 / 269264
[17.38.14] Progress: 7 Bytes Sent: 41135 / 269264
[17.38.14] Progress: 9 Bytes Sent: 49327 / 269264
[17.38.14] Progress: 10 Bytes Sent: 57519 / 269264
[17.38.14] Progress: 12 Bytes Sent: 65711 / 269264
[17.38.14] Progress: 13 Bytes Sent: 73903 / 269264
[17.38.14] Progress: 15 Bytes Sent: 82095 / 269264
[17.38.14] Progress: 16 Bytes Sent: 90287 / 269264
[17.38.14] Progress: 18 Bytes Sent: 98479 / 269264
[17.38.15] Progress: 19 Bytes Sent: 106671 / 269264
[17.38.15] Progress: 21 Bytes Sent: 114863 / 269264
[17.38.15] Progress: 22 Bytes Sent: 123055 / 269264
[17.38.15] Progress: 24 Bytes Sent: 131247 / 269264
[17.38.15] Progress: 25 Bytes Sent: 139439 / 269264
[17.38.15] Progress: 27 Bytes Sent: 147631 / 269264
[17.38.16] Progress: 28 Bytes Sent: 155823 / 269264
[17.38.16] Progress: 30 Bytes Sent: 164015 / 269264
[17.38.16] Progress: 31 Bytes Sent: 172207 / 269264
[17.38.16] Progress: 33 Bytes Sent: 180399 / 269264
[17.38.16] Progress: 35 Bytes Sent: 188591 / 269264
[17.38.16] Progress: 36 Bytes Sent: 196783 / 269264
[17.38.17] Progress: 38 Bytes Sent: 204975 / 269264
[17.38.17] Progress: 39 Bytes Sent: 213167 / 269264
[17.38.17] Progress: 41 Bytes Sent: 221359 / 269264
[17.38.17] Progress: 42 Bytes Sent: 229551 / 269264
[17.38.17] Progress: 44 Bytes Sent: 237743 / 269264
[17.38.17] Progress: 45 Bytes Sent: 245935 / 269264
[17.38.17] Progress: 47 Bytes Sent: 254127 / 269264
[17.38.18] Progress: 48 Bytes Sent: 262319 / 269264
[17.38.18] Progress: 49 Bytes Sent: 269220 / 269264
[17.38.18] Progress: 50 Bytes Sent: 269264 / 269264
[17.38.25] Progress: -50 Bytes Sent: 269264 / 269264
[17.38.25] Progress: 100 Bytes Sent: 269264 / 269264
[17.38.25] FileCompleted event raised!
Run Code Online (Sandbox Code Playgroud)

所以,在网上搜索,我发现从50-> 100的跳跃,只是百分比报告中的设计选择......所以我很好.奇怪的问题是,即使在50%(整个文件发送时),网络接口仍然会产生流量并仍在上传.事实上,正如您在上面的日志中所看到的那样,在文件发送之后需要7秒才能引发UploadFileCompletedEvent ..事实上,同时,我仍然通过HTTP发送文件.

这里的问题是我无法可靠地更新我的UI:进度条将增长到50%但是它将等待上传完成(这是一个不好的行为,因为,对于大文件,这个时间显着增长).

问题是:我如何可靠地让用户了解文件上传进度?

谢谢.

PS该方法工作得很好,文件正确上传到远程服务器.唯一的问题是进度报告.

CrA*_*HeR 2

我刚刚发现了这个问题:它出现在基本的 HTTP 身份验证中。由于某些奇怪的原因,即使我指定了凭据,WebClient 也会提交第一个 POST 请求,而不会在 HTTP 标头中指定凭据。服务器回复身份验证请求后,它会提交第二个 POST 请求,并正确指定凭据。在这 2 次重试中,我的应用程序发送了文件 2 次!(这就是为什么即使文件完全发送后我仍然经历上传活动)

解决方案是通过手动添加 Authentication 标头(并删除 WebClient.Credentials.. 行)来强制进行 HTTP 基本身份验证:

string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
Run Code Online (Sandbox Code Playgroud)

这样,第一个(也是唯一的)POST 请求将使用身份验证标头正确提交,并且报告进度正确(仅用于共享,出于上述原因,我在进度栏中将进度报告为 (e.ProgressPercentage * 2)。