使用 Webclient.Uploadfile 获取文件上传过程中的上传进度

编程入门 行业动态 更新时间:2024-10-28 16:18:13
本文介绍了使用 Webclient.Uploadfile 获取文件上传过程中的上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个使用网络客户端将文件上传到服务器的应用程序.我想在文件上传过程中显示一个进度条.我将如何实现这一目标?

I have an app that uploads files to server using the webclient. I'd like to display a progressbar while the file upload is in progress. How would I go about achieving this?

推荐答案

WebClient.UploadFileAsync 将允许您执行此操作.

WebClient.UploadFileAsync will allow you to do this.

WebClient webClient = new WebClient(); webClient.UploadFileAsync(address, fileName); webClient.UploadProgressChanged += WebClientUploadProgressChanged;

...

void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { Console.WriteLine("Upload {0}% complete. ", e.ProgressPercentage); }

请注意,该线程将不再阻塞上传,因此我建议使用:

Note that the thread won't block on Upload anymore, so I'd recommend using:

webClient.UploadFileCompleted += WebClientUploadCompleted;

...

void WebClientUploadCompleted(object sender, UploadFileCompletedEventArgs e) { // The upload is finished, clean up }

更多推荐

使用 Webclient.Uploadfile 获取文件上传过程中的上传进度

本文发布于:2023-10-12 20:40:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过程中   进度   文件上传   上传   Webclient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!