从带有进度的 FTP 下载文件 - TotalBytesToReceive 始终

Download file from FTP with Progress - TotalBytesToReceive is always -1?(从带有进度的 FTP 下载文件 - TotalBytesToReceive 始终为 -1?)

本文介绍了从带有进度的 FTP 下载文件 - TotalBytesToReceive 始终为 -1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to download a file from an FTP server with a progress bar.

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

The file is downloading, and the ProgressChanged event is calling, except in the event args TotalBytesToReceive is always -1. TotalBytes increases, but I am unable to calculate the percentage without the total.

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

I imagine I could find the file size through other ftp commands, but I wonder why this doesn't work?

我的代码:

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) ;

....

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 + "%)");
    }
}

....

class FTPClient : WebClient
{
    protected override WebRequest GetWebRequest(System.Uri address)
    {
        FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
        req.UsePassive = false;
        return req;
    }
}

谢谢.

推荐答案

所以我遇到了同样的问题.我通过首先检索文件大小来解决它.

So I had the same issue. I got around it by retrieving the file size first.

        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("URL");
        request.Method = WebRequestMethods.Ftp.GetFileSize;
        request.Credentials = networkCredential;
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        bytes_total = response.ContentLength; //this is an int member variable stored for later
        Console.WriteLine("Fetch Complete, ContentLength {0}", response.ContentLength);
        response.Close();

        webClient = new MyWebClient();
        webClient.Credentials = networkCredential; ;
        webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(FTPDownloadCompleted);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(FTPDownloadProgressChanged);
        webClient.DownloadDataAsync(new Uri("URL"));

然后在回调中进行数学运算.

Then do the math in the callback.

private void FTPDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
     progressBar.Value = (int)(((float)e.BytesReceived / (float)bytes_total) * 100.0);
}

这篇关于从带有进度的 FTP 下载文件 - TotalBytesToReceive 始终为 -1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从带有进度的 FTP 下载文件 - TotalBytesToReceive 始终

基础教程推荐