zoukankan      html  css  js  c++  java
  • C# show FTP Download/Upload progress

    https://stackoverflow.com/questions/4591059/download-file-from-ftp-with-progress-totalbytestoreceive-is-always-1


    With FTP protocol, WebClient in general does not know total download size. So you commonly get -1 with FTP.

    Note that the behavior actually contradicts the .NET documentation, which says for FtpWebResponse.ContentLength (where the value of TotalBytesToReceive comes from):

    For requests that use the DownloadFile method, the property is greater than zero if the downloaded file contained data and is zero if it was empty.
    But you will easily find out many of questions about this, effectively showing that the behavior is not always as documented. The FtpWebResponse.ContentLength has a meaningful value for GetFileSize method only.

    The FtpWebRequest/WebClient makes no explicit attempt to find out a size of the file that it is downloading. All it does is that it tries to look for (xxx bytes). string in 125/150 responses to RETR command. No FTP RFC mandates that the server should include such information. ProFTPD (see data_pasv_open in src/data.c) and vsftpd (see handle_retr in postlogin.c) seem to include this information. Other common FTP servers (IIS, FileZilla) do not do this.

    If your server does not provide size information, you have to query for size yourself before download. A complete solution using FtpWebRequest and Task:

    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:localpathfile.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

    The core download code is based on:
    Upload and download a binary file to/from FTP server in C#/.NET

  • 相关阅读:
    Netty章节二十三:Netty自定义实现粘包与粘包
    Netty章节二十二:Netty自定义编解码器
    Netty章节二十一:Netty的ByteBuf
    Netty章节二十:Netty中的理论与实践
    Netty章节十八:Netty Server Start 源码分析
    Netty章节十七:Zero-copy,零拷贝
    Netty章节十六:Java NIO
    Netty章节十五:Nodejs使用gRPC与Java进行远程通信
    UML类图
    Java中虚函数和纯虚函数
  • 原文地址:https://www.cnblogs.com/wgscd/p/10142095.html
Copyright © 2011-2022 走看看