C# 中使用 WebClient 进行 FTP 文件上传的进展

huangapple go评论78阅读模式
英文:

Progress in FTP file upload with WebClient in C#

问题

以下是翻译好的部分:

我有以下代码可以正常工作 - 上传文件,但既没有进度事件也没有完成事件触发。有什么想法为什么?

try
{
    string srcFilePath = @"C:\Projects\MySetup.zip";
    string url = "ftp://xxx.xxx.xxx.xxx/downloads/MySetup.zip";

    Uri uri = new Uri(url);

    WebClient client = new WebClient();
    client.Credentials = new NetworkCredential("userName", "pass");

    client.DownloadFileCompleted += Client_DownloadFileCompleted;
    client.DownloadProgressChanged += Client_DownloadProgressChanged; 
                
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

    client.UploadFileAsync(uri, srcFilePath);
}
catch(System.Net.WebException e)
{
    Console.WriteLine(e.Message + ": " + e.Status);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

private static void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine(String.Format("完成百分比:{0}", e.ProgressPercentage));
}

private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    doneEvent.Set();
}
英文:

I have the following code that works - uploads the file, but neither progress nor completion events are fired. Any ideas why?

try
{
    string srcFilePath = @"C:\Projects\MySetup.zip";
    string url = "ftp://xxx.xxx.xxx.xxx/downloads/MySetup.zip";

    Uri uri = new Uri(url);

    WebClient client = new WebClient();
    client.Credentials = new NetworkCredential("userName", "pass");

    client.DownloadFileCompleted += Client_DownloadFileCompleted;
    client.DownloadProgressChanged += Client_DownloadProgressChanged; 
                
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

    client.UploadFileAsync(uri, srcFilePath);
}
catch(System.Net.WebException e)
{
    Console.WriteLine(e.Message + ": " + e.Status);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

private static void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine(String.Format("Percent complete: {0}", e.ProgressPercentage));
}

private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    doneEvent.Set();
}

I tried the above code and expected that the event will fire and will provide feedback about completion and progress...

答案1

得分: 0

在你的代码中,你正在使用 UploadFileAsync 方法来将文件上传到服务器。然而,由于这是一个上传操作,DownloadProgressChangedDownloadFileCompleted 事件不会被触发,因为它们是设计用来跟踪下载操作的。

你应该使用 WebClient.UploadProgressChanged 事件代替 DownloadProgressChanged,以及 WebClient.UploadFileCompleted 事件代替 DownloadFileCompleted。这些事件专门设计用于跟踪上传进度:

client.UploadFileCompleted += Client_UploadFileCompleted;
client.UploadProgressChanged += Client_UploadProgressChanged; 

...
private static void Client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
    Console.WriteLine(String.Format("上传进度:{0}%", e.ProgressPercentage));
}

private static void Client_UploadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    doneEvent.Set();
}  

UploadFileCompleted 在每次异步文件上传操作完成时触发。异步文件上传是通过调用 UploadFileAsync 方法来启动的。

UploadProgressChanged 在每次异步上传操作取得进展时触发。此事件在使用以下任一方法启动上传时触发:UploadDataAsyncUploadFileAsyncUploadValuesAsync

英文:

In your code, you are using UploadFileAsync method to upload a file to the server. However, since it is an upload operation, the DownloadProgressChanged and DownloadFileCompleted events will not be fired because they are designed for tracking download operations.

You should use WebClient.UploadProgressChanged event instead of DownloadProgressChanged and WebClient.UploadFileCompleted event instead of DownloadFileCompleted. This events are specifically designed for tracking upload progress:

client.UploadFileCompleted += Client_UploadFileCompleted;
client.UploadProgressChanged += Client_UploadProgressChanged; 

...
private static void Client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
    Console.WriteLine(String.Format("Percent complete: {0}", e.ProgressPercentage));
}

private static void Client_UploadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    doneEvent.Set();
}  

> UploadFileCompleted is raised each time an asynchronous file upload operation completes. Asynchronous file uploads are started by calling the UploadFileAsync methods.

> UploadProgressChanged is raised each time an asynchronous upload makes progress. This event is raised when uploads are started using any of the following methods: UploadDataAsync, UploadFileAsync, UploadValuesAsync.

huangapple
  • 本文由 发表于 2023年2月24日 04:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定