我需要一些帮助奇怪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一个进度条。 第一个问题是,进度百分比表示,与任何文件大小为:
[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跳,只是一个设计选择的百分比report..and所以我与它的罚款。 奇怪的问题是,即使在50%(当整个文件被发送),网络接口仍然产生流量,并仍在上传。 事实上,你可以从上面的日志中看到的时间,需要7秒,文件发出后,提高UploadFileCompletedEvent..in事实,同时,我还是发送文件通过HTTP。
这里的问题是,我不能可靠地更新我的UI:进度条将增长至50%,但随后会被卡住等待上传completition(这是因为,与大文件,此时显著生长着一种不良行为)。
现在的问题是:如何能够可靠地保持更新有关文件上传进度用户?
谢谢。
PS方法工作完全正常,文件被正确上传到远程服务器。 唯一的问题是与进度报告。