我需要一些奇怪的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);
}
}
在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!
所以,在网上搜索,我发现从50-> 100的跳跃,只是百分比报告中的设计选择......所以我很好。 奇怪的问题是即使在50%(整个文件发送时),网络接口仍然会产生流量并仍在上传。 事实上,从上面的日志中可以看出,文件发送后需要7秒才能引发UploadFileCompletedEvent ..事实上,我仍然通过HTTP发送文件。
这里的问题是我无法可靠地更新我的UI:进度条将增长到50%但是它将等待上传完成(这是一个不好的行为,因为,对于大文件,这个时间显着增长)。
问题是:我如何可靠地让用户了解文件上传进度?
感谢。
P.S。该方法工作正常,文件正确上传到远程服务器。唯一的问题是进度报告。
答案 0 :(得分:2)
我刚刚发现了这个问题:它在基本的HTTP身份验证中。出于某些奇怪的原因,即使我指定凭据,WebClient也会提交第一个POST请求,而不指定HTTP标头中的凭据。服务器使用auth-request回复后,它会提交第二个POST请求,正确地指定凭据。在这2次重试中,我的应用程序发送文件2次! (这就是我在文件完全发送后经历上传活动的原因)
解决方案是通过手动添加Authentication头强制HTTP基本身份验证(并删除WebClient.Credentials ..行:
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
通过这种方式,第一个(也是唯一的)POST请求将使用Authentication标头正确提交,并且报告进度正确(仅用于共享,我将进度条中的进度报告为(e.ProgressPercentage * 2),原因是:上方。
答案 1 :(得分:1)
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
将此信息放在发布请求的开头,并e.ProgressPercentage * 2
用于进度报告。