简体   繁体   English

使用Webclient.UploadFileAsync一对一上传文件

[英]Uploading files one by one using Webclient.UploadFileAsync

我正在将多个文件发送到我的包装器,该包装器将文件发送到ftp。我必须获取上载进度,所以我必须使用asycn方法进行上载。问题是,如何才能一一调用上载器,但是还获得进度报告?

Here's a solution if you want to wait for each file: 如果您要等待每个文件,这是一个解决方案:

class FileUploader
{
    private readonly Uri _destination;

    public FileUploader(Uri destination)
    {
        _destination = destination;
    }

    public void UploadFiles(IEnumerable<string> fileNames)
    {
        foreach (var fileName in fileNames)
        {
            UploadFile(fileName);
        }
    }

    private void UploadFile(string fileName)
    {
        var tcs = new TaskCompletionSource<bool>();
        using (var client = new WebClient())
        {
            client.UploadProgressChanged += UploadProgressChangedHandler;
            client.UploadFileCompleted += (sender, args) => UploadCompletedHandler(fileName, tcs, args);
            client.UploadFileAsync(_destination, fileName);
            tcs.Task.Wait();
        }
    }

    private void UploadCompletedHandler(string fileName, TaskCompletionSource<bool> tcs, UploadFileCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            tcs.TrySetCanceled();
        }
        else if (e.Error != null)
        {
            tcs.TrySetException(e.Error);
        }
        else
        {
            tcs.TrySetResult(true);
        }
    }

    private void UploadProgressChangedHandler(object sender, UploadProgressChangedEventArgs e)
    {
        // Handle progress, e.g.
        System.Diagnostics.Debug.WriteLine(e.ProgressPercentage);
    }
}

You have to subscribe to the UploadProgressChanged event: 您必须订阅UploadProgressChanged事件:

var client = new WebClient();
client.UploadProgressChanged += (s, e) => System.Diagnostics.Debug.WriteLine(e.ProgressPercentage);
client.UploadFileAsync(new Uri("ftp://server/directory"), @"C:\temp\file.txt");
client.UploadFileCompleted += (s, e) => Task.Factory.StartNew(client.Dispose);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用WebClient.UploadFileAsync来上传文件和POST参数? - How to use WebClient.UploadFileAsync to upload files and POST Params as well? 为什么WebClient.UploadFileAsync中的UploadProgressChanged无法正常工作? - Why UploadProgressChanged in WebClient.UploadFileAsync work not correctly? WebClient.UploadFileAsync - 上传完成后删除 - WebClient.UploadFileAsync - delete after upload completion WebClient.UploadFileASync的非常奇怪的行为 - Very strange behaviour with WebClient.UploadFileASync 如何提高Webclient.UploadFileAsync函数的速度? - How to increase speed of Webclient.UploadFileAsync function? 使用WebClient.UploadFileAsync方法获取服务器响应 - Get server response with WebClient.UploadFileAsync method 取消webclient.UploadFileAsync()原因TargetInvocationException错误 - Cancel webclient.UploadFileAsync() casue TargetInvocationException error 使用WebClient.UploadFileAsync时文件未上载到某些计算机上的FTP服务器 - File is not uploaded to FTP server on some machines when using WebClient.UploadFileAsync 为什么 WebClient.UploadFileAsync 不适用于大文件上传? - Why is WebClient.UploadFileAsync not working with large file uploads? C#WebClient.UploadFileAsync无法访问文件,因为它正在被另一个进程使用 - C# WebClient.UploadFileAsync Cannot access the file because it's being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM