![](/img/trans.png)
[英]Should I check the response of WebClient.UploadFile to know if the upload was successful?
[英]Getting the upload progress during file upload using Webclient.Uploadfile
我有一个应用程序,使用webclient将文件上传到服务器。 我想在文件上传过程中显示进度条。 我将如何实现这一目标?
WebClient.UploadFileAsync将允许您执行此操作。
WebClient webClient = new WebClient();
webClient.UploadFileAsync(address, fileName);
webClient.UploadProgressChanged += WebClientUploadProgressChanged;
...
void WebClientUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
Console.WriteLine("Upload {0}% complete. ", e.ProgressPercentage);
}
请注意,该线程将不再阻止上传,所以我建议使用:
webClient.UploadFileCompleted += WebClientUploadCompleted;
...
void WebClientUploadCompleted(object sender, UploadFileCompletedEventArgs e)
{
// The upload is finished, clean up
}
将事件处理程序添加到WebClient.UploadProgressChanged并调用WebClient.UploadFileAsync。
有关示例,请参阅WebClient.UploadProgressChanged文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.