WebClient with Progress bar

I am writing a small application which allows user to enter an URL then download a file on the net. In addition, I use the progress bar to show the downloading status. These code as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;


namespace DowloaderProgress
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdDownload_Click(object sender, EventArgs e)
        {
            this.prgDownload.Maximum = 100;
            WebClient myClient = new WebClient();
            Uri url = new Uri(txtURL.Text);
            string file = "text.pdf";
            myClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
            myClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallBack2);
            //myClient.DownloadFileAsync(url, file);
          
        }
        void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
        {
           this.prgDownload.Value = e.ProgressPercentage;
           this.lblSize.Text = e.TotalBytesToReceive.ToString();
           this.lblReceived.Text = e.BytesReceived.ToString();
           this.lblPercent.Text = e.ProgressPercentage.ToString();
        }

        void DownloadFileCallBack2(object sender, AsyncCompletedEventArgs c)
        {
            MessageBox.Show("Download Completed");
        }
    }
}

However, I got the problem. If the file size is big enough, it can call the DownloadProgressCallback method so the progress bar could work, but if the file size is very small (downlading in cable network), it just call the DownloadFileCallBack2 method only. Anyone has any suggestion

Thanks in advanced,

Chris


Answer this question

WebClient with Progress bar

  • DotNetSteve

    I check again, and even when the file is downloaded

    DownloadProgressCallback is never called. as the progress bar, and all the associated labels are never updated.



  • hYp3r



    How would you add error trapping to the code above I know the server is returning an error not found message and the program crashes. How do I catch this

  • dummies

    i have do the same like you but if a file is big for ex. 200 kb

    Before and during:

    e.ProgressPercentage = 0
    e.TotalBytesToReceive.ToString() = -1;

    Then when the download is terminated:

    e.ProgressPercentage = 100;
    e.TotalBytesToReceive.ToString() = 200 kb;

    but during the download this 2 properties don't work


  • JonS123

    Hi,man

    I have the problem too.

    WebClient WebIni;
    Uri url;
    WebIni = new WebClient();
    WebIni.Credentials = new NetworkCredential("qyy","74123698"); //username and password
    url = new Uri("ftp://192.168.2.6/UFDATA.MDF");
    WebIni.DownloadProgressChanged += new DownloadProgressChangedEventHandler(WebIni_DownloadProgressChanged);
    WebIni.DownloadFileCompleted += new AsyncCompletedEventHandler(WebIni_DownloadFileCompleted);
    WebIni.DownloadFileAsync (url, "c:\\ww.dat");
    }

    void WebIni_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
    this.Text = "revd:" + e.BytesReceived.ToString() + " total:"
    + e.TotalBytesToReceive.ToString() + " Percent:"
    + e.ProgressPercentage.ToString()+"%";//
    }

    void WebIni_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
    MessageBox.Show("download ok");
    }

    I don't understand why e.TotalBytesToReceive is always -1,but BytesReceived will increase.

    Who knows



  • Mareen Philip

    f
  • kingdeadbo

    Which beta version of the Whidbey framework are you using If you are using beta2 this bug should have been fixed (I think). I am absolutely sure that for the RTM version the download progress even will be called atleast once and that callback can have 100% percentage when called which will be the case when a small file is downloaded quickly.

    Mahesh

  • bobmarley165735

    I'm using Visual Studio 2005, Professional edition,

    I've implemented this download manager, and, in my machine, the progress bar never moves, even though the file is being downloaded and in the directory the file size is increasing, however the progress bar is not moving.

    Do I have to implement that form as a seperate thread I think when the download starts the form gets jammed or something, and when I try to quit it it says (not responding) and none of the controls on the form change during the download.

    Altough lblsize, lblprogress etc. should be updated while the download proceeds.



  • arthurmnev

    Please reply to this thread in the future if the problem occurs with the final version.

  • Norman Sasono

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;


    namespace DowloaderProgress
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    // In the following procedure:
    // from Microsoft MSDN Online Documentation:
    // working sample url: http://www.contoso.com/logs/
    // working file: January.txt
    private void cmdDownload_Click(object sender, EventArgs e)
    {
    this.prgDownload.Maximum = 100;
    WebClient myClient = new WebClient();
    Uri url = new Uri(this.txtURL.Text);
    string file = "January.txt";

    myClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);

    myClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallBack2);

    myClient.DownloadFileAsync(url, file);
    }

    void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
    {
    this.prgDownload.Value = e.ProgressPercentage;
    this.lblSize.Text = e.TotalBytesToReceive.ToString();
    this.lblReceived.Text = e.BytesReceived.ToString();
    this.lblPercent.Text = e.ProgressPercentage.ToString();
    }

    void DownloadFileCallBack2(object sender, AsyncCompletedEventArgs c)
    {
    MessageBox.Show("Download Completed");
    }
    }

    From:
    new DownloadProgressChangedEventArgs(DownloadProgressCallback);

    To:
    new DownloadProgressChangedEventHandler(DownloadProgressCallback);


    I've tested the above code and it is now updating the label.

    Regards Dat.


  • WebClient with Progress bar