WebClient AsyncUpload 进度百分比始终返回 50%
我正在使用 Webclient 通过异步调用将数据上传到服务器,
WebClient webClient = new WebClient();
webClient.UploadDataAsync(uri , "PUT", buffer, userToken);
我已将 DatauploadProgress 和 DatauploadCompleted 事件附加到适当的回调函数,
// Upload Date Progress
webClient.UploadProgressChanged += new
UploadProgressChangedEventHandler(UploadProgressCallback);
// Upload Date Progress
void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
// Magic goes here
logger.writeToLog("Percentage =" + e.ProgressPercentage);
}
e.ProgressPercentage
始终返回 50
.. 否无论上传的文件大小是多少(尝试了 10kb 到 60mb 之间的不同大小)。 函数本身只被调用两次(也非常快)并且百分比显示 50! ..这是不合逻辑的,特别是对于大文件...
e.BytesSent
也没有帮助..它总是以字节为单位显示文件大小:S(例如:如果文件大小为 63,000 ,我会得到 e.BytesSent = 63,000
和 e.ProgressPercentage= 50
有人能向我指出问题吗?
i am using Webclient to upload data using Async call to a server,
WebClient webClient = new WebClient();
webClient.UploadDataAsync(uri , "PUT", buffer, userToken);
i've attached DatauploadProgress and DatauploadCompleted Events to appropriate callback functions
// Upload Date Progress
webClient.UploadProgressChanged += new
UploadProgressChangedEventHandler(UploadProgressCallback);
// Upload Date Progress
void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
// Magic goes here
logger.writeToLog("Percentage =" + e.ProgressPercentage);
}
the e.ProgressPercentage
always returns 50
.. no matter what the size of the file uploaded was (tried different sizes between 10kb to 60mb ).
the function itself gets called only twice (really fast too) and percentage shows 50! ..which is illogical specially with big files ...
e.BytesSent
doesn't help either..it always shows the files size in bytes :S (ex: if the file size was 63,000 , i'd get e.BytesSent = 63,000
and e.ProgressPercentage= 50
Can someone point the problem out to me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要监视上传进度,则需要使用 UploadFileAsync 而不是 UploadData。
使用UploadDataAsync,您应该手动对文件进行分块并显示进度(至少,这是我根据自己的经验确定的,尽管我还没有在MSDN 上看到这样的写法)。
您正在寻找的是使用 UploadFileAsync 来代替,它将正确调用 UploadProgressChanged 事件。然后,您可以查看应该正确反映的事件参数属性 BytesSent 和 TotalBytesToSend。
我认为这背后的基本原理是,当您发送数据时,您可以循环数据流的块并手动增加进度跟踪器,而对于文件则不能(.NET 将管理整个数据流)为您上传)。就我个人而言,我觉得有些可疑,因为在 UploadDataAsync 的情况下,没有理由使用无效信息来调用 UploadProgressChanged 事件 - 要么使用有效、正确的信息调用它,要么根本不调用它。
无论如何,尝试一下 UploadFileAsync,看看效果如何。
If you want to monitor the progress of an upload, you'll need to use UploadFileAsync instead of UploadData.
With UploadDataAsync you are supposed to manually chunk the file and display the progress (at least, that's what I've determined from my own experience in the matter though I haven't seen it written as such on MSDN).
What you're looking for is to use UploadFileAsync instead, which will call the UploadProgressChanged event correctly. You can then view the event args properties BytesSent and TotalBytesToSend which should be reflected correctly.
I assume the rationale behind this is that when you're sending data, you can loop over chunks of your data stream and manually increment your progress tracker whereas with a file you cannot (.NET will manage the entire upload for you). Personally, I feel there is something fishy because there is no reason for the UploadProgressChanged event to be called with invalid information in case of UploadDataAsync - either it is called with valid, correct info or it's not called at all.
At any rate, give UploadFileAsync a shot and see how that goes.