WebClient.UploadValuesAsync 未正确更新进度

发布于 2024-10-13 02:07:58 字数 1286 浏览 3 评论 0原文

我有一个 WebClient,我用它来按以下方式上传文件,base64Encoded 是编码为 base64 字符串的图片,这正是 imgur 服务器所期望的:

    public Upload()
    {
        WebClient webClient = new WebClient();
        webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(webClient_UploadProgressChanged);
        webClient.UploadValuesCompleted += new UploadValuesCompletedEventHandler(webClient_UploadValuesCompleted);    

        NameValueCollection values = new NameValueCollection();
        values.Add("key", "imgur api key");
        values.Add("image", base64Encoded);
        webClient.UploadValuesAsync(new Uri("http://api.imgur.com/2/upload"), "POST", values);        
    }

这是 UploadProgressChanged 的​​事件处理程序:

    private void webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
    {
        int percentage = e.ProgressPercentage * 2;

        progressBar.Value = percentage;
        percentageTextBlock.Text = (percentage).ToString() + "%";
    }

现在我的问题是该事件handler 仅在开始时调用一次,报告 ProgressPercentage 为 50,然后不再调用。文件上传成功,但我的进度条不起作用。这并不是因为我上传的是一个小文件,因为我也尝试过使用几个 mb 的文件,这些文件也立即报告 ProgressPercentage 为 50。 e.BytesSent 也没有帮助,因为它也立即等于 e.TotalBytesToSend 。这是怎么回事?

I have a WebClient which I use to upload a file in the following way, base64Encoded is a picture encoded as a base64 string as that is what the imgur server expects:

    public Upload()
    {
        WebClient webClient = new WebClient();
        webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(webClient_UploadProgressChanged);
        webClient.UploadValuesCompleted += new UploadValuesCompletedEventHandler(webClient_UploadValuesCompleted);    

        NameValueCollection values = new NameValueCollection();
        values.Add("key", "imgur api key");
        values.Add("image", base64Encoded);
        webClient.UploadValuesAsync(new Uri("http://api.imgur.com/2/upload"), "POST", values);        
    }

This is the event handler for the UploadProgressChanged:

    private void webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
    {
        int percentage = e.ProgressPercentage * 2;

        progressBar.Value = percentage;
        percentageTextBlock.Text = (percentage).ToString() + "%";
    }

Now my problem is that the event handler is only called once right at the start, reports a ProgressPercentage of 50 and is then not being called anymore. The file uploads successfully, but my progressbar is not working. This is not because I'm uploading a small file as I've also tried this with files of several mb which also report a ProgressPercentage of 50 right away. e.BytesSent is no help either because that one is equal to e.TotalBytesToSend right away as well. What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-10-20 02:07:58

There was a bug with this event which was fixed in .NET 4.0. And here's a related post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文