Web客户端AsyncUpload进度百分比总是返回50%百分比、进度、客户端、Web

由网友(心哇凉哇凉的)分享简介:我使用Web客户端上传使用异步调用到服务器的数据,i am using Webclient to upload data using Async call to a server,WebClient webClient = new WebClient();webClient.UploadDataAsync(uri ,...

我使用Web客户端上传使用异步调用到服务器的数据,

i am using Webclient to upload data using Async call to a server,

    WebClient webClient = new WebClient();
   webClient.UploadDataAsync(uri , "PUT", buffer, userToken);

我已经把它贴DatauploadProgress和DatauploadCompleted活动,以适当的回调函数

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);
    }

e.ProgressPercentage 总是返回 50 ..不管发生了什么(试着上传文件的大​​小10KB之间不同尺寸为60MB)。 函数本身被称为只有两次(真快得)和百分比显示50! ..这是不合逻辑的特别大的文件...

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 并不能帮助either..it始终显示文件的字节大小:S(例如:如果文件大小为63,000,我会得到 e.BytesSent = 63000 e.ProgressPercentage = 50

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,000and e.ProgressPercentage= 50

有人能指出问题出在我身上?

Can someone point the problem out to me ?

推荐答案

如果你想监控上传的进度,你需要使用,而不是UploadData UploadFileAsync。

If you want to monitor the progress of an upload, you'll need to use UploadFileAsync instead of UploadData.

使用UploadDataAsync你应该手动块文件,并显示进度(至少,这就是我从在这个问题上我自己的经验判断,虽然我还没有看到它写成这样MSDN上)。

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).

您正在寻找的是使用UploadFileAsync相反,它会正确调用UploadProgressChanged事件。然后,您可以查看事件参数性能BytesSent和TotalBytesToSend应正确反映。

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.

我认为这背后的理由是,当你发送数据,则可以遍历数据流的块和手动增加你的进度跟踪,而你不能(.NET将管理的整个文件的上传你)。就个人而言,我觉得有猫腻,因为没有理由UploadProgressChanged事件被调用的无效的情况下UploadDataAsync的信息 - 无论它被称为具有有效的,正确的信息,或者它不叫所有

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.

在任何情况下,给UploadFileAsync一个镜头,看看如何继续下去。

At any rate, give UploadFileAsync a shot and see how that goes.

阅读全文

相关推荐

最新文章