小编CrA*_*HeR的帖子

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

我需要一些奇怪的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 / …
Run Code Online (Sandbox Code Playgroud)

c# system.net webclient

10
推荐指数
1
解决办法
2180
查看次数

标签 统计

c# ×1

system.net ×1

webclient ×1