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

Mit*_*tch 7 c# ftp asynchronous webclient

我正在尝试从带有进度条的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 override WebRequest GetWebRequest(System.Uri address)
    {
        FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
        req.UsePassive = false;
        return req;
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

kyn*_*igs -1

FTP 不会像 HTTP 那样为您提供内容大小,您可能最好自己做这件事。

FtpWebRequest FTPWbReq = WebRequest.Create("somefile") as FtpWebRequest;
FTPWbReq .Method = WebRequestMethods.Ftp.GetFileSize;

FtpWebResponse FTPWebRes = FTPWbReq.GetResponse() as FtpWebResponse;
long length = FTPWebRes.ContentLength;
FTPWebRes.Close();
Run Code Online (Sandbox Code Playgroud)