加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WebClient UploadFileAsync异常行为进行中的报告

发布时间:2020-12-15 06:28:12 所属栏目:百科 来源:网络整理
导读:我需要一些帮助与奇怪的WebClient.UploadFileAsync()的行为. 我正在将文件上传到远程HTTP Server(nginx)usind POST方法. POST通过 PHP脚本(该地址引用)进行处理. 我有这个简单的代码 public void uploadFile(string filePath){ webClient = new WebClient();
我需要一些帮助与奇怪的WebClient.UploadFileAsync()的行为.
我正在将文件上传到远程HTTP Server(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我只是更新ProgressBar与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的跳跃只是百分比报告中的设计选择,所以我很好.
奇怪的问题是,即使在50%(整个文件发送的时候),网络接口仍然产生流量并仍在上传.
事实上,从上面的日志中可以看出,在发送文件之后,需要7秒钟的时间来提高UploadFileCompletedEvent ..事实上,我仍然通过HTTP发送文件.

这里的问题是,我无法可靠地更新我的用户界面:进度条将增长到50%,但是它将被卡住等待上传完成(这是一个不好的行为,因为大文件,这一次显着增长).

问题是:我如何可靠地保持用户更新有关文件上传进度?

谢谢.

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

解决方法

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

您的发布请求开始时,请使用e.ProgressPercentage * 2进行报告.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读