Progress bar does not update with ftp download

Asked

Viewed 80 times

0

Hello, so here’s the thing i have a function that downloads the desired file from http, and as it downloads, it updates the progress bar (being increased from 0% little to 100% when finished).

System.IO.Directory.CreateDirectory(@"C:\...\Img");
// Creates a webclient
System.Net.WebClient webClient = new System.Net.WebClient();
// Uses the Event Handler to check whether the download is complete
webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Completed);
// Uses the Event Handler to check for progress made
webClient.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(DownloadProgressChanged);
// Defines the URL and destination directory for the downloaded file
webClient.DownloadFileAsync(new Uri("http://link.../google2.0.0.jpg"), @"C:\...\Img\google2.0.0.jpg");

But I needed you to download from ftp and when I add the following lines, it doesn’t update the progress bar as you download it, it’s only updated when it’s over (it changes from 0% to 100%)

webClient.Credentials = new System.Net.NetworkCredential(ftpUserID , ftpUserPassword);
webClient.DownloadFileAsync(new Uri("ftp://[email protected]/.../file.extension"), @"C:\...\Img\file.extension");

If anyone knows how to help solve it, I’d appreciate it.

2 answers

0

private void startDownload()
        {
                WebClient client = new WebClient();
            
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://github.com/JPPlaysGamer/IndustriesExes.Inc/releases/download/ieip-pre2/IEIPv0.2-alpha-Windows.zip"), temp + "\\IEIPv0.2-alpha-Windows.zip");

                

        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                double bytesIn = double.Parse(e.BytesReceived.ToString());
                double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                double percentage = bytesIn / totalBytes * 100;
                Mbs.Text = (bytesIn / 1024 / 1024).ToString("F2") + " / " + (totalBytes / 1024/ 1024).ToString("F2") + " Mb";
                Complete.Text = "Status: Downloading...";
                IEIPProgress.Value = int.Parse(Math.Truncate(percentage).ToString());
                this.Text = "IEIP Installer - " + IEIPProgress.Value + "%";
                
            });
        }
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
                {
                    File.Move(temp + "\\IEIPv0.2-alpha-Windows.zip",  localProg + "\\IEIPv0.2-alpha-Windows.zip");
                }
                
                Complete.Text = "Status: Completed";
                
                btnNo.Text = "Exit";
                btnNo.Enabled = true;
                
            });
}

See this code on the Downloadfileasync. As this method does not block main thread you can create two events mentioned above.

To do this percentage, you must first create a variable to receive the received bytes, total file size, and for the percentage

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                double bytesIn = double.Parse(e.BytesReceived.ToString());
                double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                double percentage = bytesIn / totalBytes * 100;
                Mbs.Text = (bytesIn / 1024 / 1024).ToString("F2") + " / " + (totalBytes / 1024/ 1024).ToString("F2") + " Mb";
                Complete.Text = "Status: Downloading...";
                IEIPProgress.Value = int.Parse(Math.Truncate(percentage).ToString());
                this.Text = "IEIP Installer - " + IEIPProgress.Value + "%";

            });
        }

As this example is of a file of mine it is of many MB so I do a division of bytes received by 1024, the size of KB, MB, GB , ...

I do it twice because I want to show a megabyte but if your item is Kilobyte only once. Then make received bytes divided by total bytes times 100, when adding this value to the progressibar use Math.Truncate(percentage). Use toString to make your decimal formatted. Example: toString("F2"), this indicates that it will have two decimal places.

In your form (or console), make your design you like best and prefer. Finally create an event if the download was complete:

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
                {
                    File.Move(temp + "\\IEIPv0.2-alpha-Windows.zip",  localProg + "\\IEIPv0.2-alpha-Windows.zip");
                }

                Complete.Text = "Status: Completed";

                btnNo.Text = "Exit";
                btnNo.Enabled = true;

            });
}

If your download was successful create an if to check if the file has the same bytes as the one in the URL. If you don’t have this size create an error message. Do a good file control or better create a temp.

-1


The progress bar does not update because the total bytes of a file downloaded by ftp is read as -1 by the system.. You have to read the total size and put in the formula to get the percentage

[Edit] I forgot to mark this answer as the solution. By reading the full file size then it is already possible to update the progress bar correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.