小编Mit*_*tch的帖子

使用进度从FTP下载文件 - TotalBytesToReceive始终为-1?

我正在尝试从带有进度条的FTP服务器下载文件.

文件正在下载,并且ProgressChanged事件正在调用,除非事件args TotalBytesToReceive始终为-1.TotalBytes增加,但我无法计算没有总数的百分比.

我想我可以通过其他ftp命令找到文件大小,但我想知道为什么这不起作用?

我的代码:

FTPClient request = new FTPClient();
request.Credentials = credentials;
request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(request_DownloadProgressChanged);
//request.DownloadDataCompleted += new DownloadDataCompletedEventHandler(request_DownloadDataCompleted);
request.DownloadDataAsync(new Uri(folder + file));
while (request.IsBusy) ;
Run Code Online (Sandbox Code Playgroud)

....

static void request_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    if (e.TotalBytesToReceive == -1)
    {
        l.reportProgress(-1, FormatBytes(e.BytesReceived) + " out of ?" );
    }
    else
    {
        l.reportProgress(e.ProgressPercentage, "Downloaded " + FormatBytes(e.BytesReceived) + " out of " + FormatBytes(e.TotalBytesToReceive) + " (" + e.ProgressPercentage + "%)");
    }
}
Run Code Online (Sandbox Code Playgroud)

....

class FTPClient : WebClient
{
    protected …
Run Code Online (Sandbox Code Playgroud)

c# ftp asynchronous webclient

7
推荐指数
1
解决办法
9077
查看次数

标签 统计

asynchronous ×1

c# ×1

ftp ×1

webclient ×1