从FTP下载文件时,TotalBytesToReceive始终为-1?

8

我正在尝试从FTP服务器下载文件并显示进度条。

文件正在下载,ProgressChanged事件被调用,但是在事件参数中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) ;

....

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

感谢。

看起来你需要提供一个更好的WebClient实现来处理它。寻找有趣的属性/方法进行重写。 - leppie
看了一下 WebClient,但似乎很难在没有大量 hack 的情况下实现。 - leppie
3个回答

4

我曾经遇到过同样的问题。我通过先获取文件大小来解决了这个问题。

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

然后在回调函数中进行计算。

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

2
使用FTP协议时,一般情况下WebClient无法知道总下载大小。因此,您通常会在FTP中得到-1
请注意,这种行为实际上与.NET文档相矛盾,该文档针对FtpWebResponse.ContentLength(其中TotalBytesToReceive的值来自)表示:

对于使用DownloadFile方法的请求,如果下载的文件包含数据,则该属性大于零,如果为空,则为零。

但是,您会轻松发现许多关于此问题的问题,有效地显示行为并非始终如文档所述。对于GetFileSize方法,FtpWebResponse.ContentLength仅具有有意义的值。

FtpWebRequest/WebClient没有明确尝试找出所下载的文件的大小。它所做的仅仅是尝试在RETR命令的125/150响应中查找(xxx bytes)字符串。没有FTP RFC规定服务器应包括这些信息。ProFTPD(参见src/data.c中的data_pasv_open)和vsftpd(参见postlogin.c中的handle_retr)似乎包含了这些信息。其他常见的FTP服务器(IIS、FileZilla)则不会这样做。


如果您的服务器没有提供文件大小信息,您需要在下载之前自己查询文件大小。使用FtpWebRequestTask的完整解决方案如下:
private void button1_Click(object sender, EventArgs e)
{
    // Run Download on background thread
    Task.Run(() => Download());
}

private void Download()
{
    try
    {
        const string url = "ftp://ftp.example.com/remote/path/file.zip";
        NetworkCredential credentials = new NetworkCredential("username", "password");

        // Query size of the file to be downloaded
        WebRequest sizeRequest = WebRequest.Create(url);
        sizeRequest.Credentials = credentials;
        sizeRequest.Method = WebRequestMethods.Ftp.GetFileSize;
        int size = (int)sizeRequest.GetResponse().ContentLength;

        progressBar1.Invoke(
            (MethodInvoker)(() => progressBar1.Maximum = size));

        // Download the file
        WebRequest request = WebRequest.Create(url);
        request.Credentials = credentials;
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        using (Stream ftpStream = request.GetResponse().GetResponseStream())
        using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
        {
            byte[] buffer = new byte[10240];
            int read;
            while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                fileStream.Write(buffer, 0, read);
                int position = (int)fileStream.Position;
                progressBar1.Invoke(
                    (MethodInvoker)(() => progressBar1.Value = position));
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

enter image description here

核心下载代码基于:
C#/.NET中上传和下载二进制文件到/从FTP服务器

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接