WebClient.UploadValuesAsync not updating progress properly

孤者浪人 提交于 2019-12-11 17:17:54

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4768419/webclient-uploadvaluesasync-not-updating-progress-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!