Ehsan
Ehsan

Reputation: 4464

C# How to get the size of the download file for showing the progressbar percentage

I'm using progressbar in c# but I got a bit of a problem I'm connecting to a server in our company and then I start to download an XML file from that 4k by 4k and showing this progress on a progressbar and for showing this if the file is below 100K till now then the maximum size of my progressbar would be 100 and if it changes I would change the maximum size of it to 1000 then 10000 and etc, so by this way I can doing this

progressbar1.value += 4;

So it's somehow working, but as all you know the right solution is to know the size of the file my connection is going to download before starting to download that. I think a little and I think using Curl maybe solve the problem, but I don't know exactly how to use it in C# or is there anyway to solve this problem.

Edit: I'm downloading the file like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Credentials = new NetworkCredential(usr, pass);
            ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader s = new StreamReader(response.GetResponseStream());
            int i = 0;
            int j = 0;
            int lastIndex = 0;
            int readNum = 0;
            Console.WriteLine("start at : " + DateTime.Now);
            while (!s.EndOfStream)
            {
                readNum = s.ReadBlock(temp, 0, 4096);
                if (w != null)
                    w.incByVolume(4);
                ret += new String(temp);
                temp = new char[4096];
            }

Thanks in advance...

Upvotes: 2

Views: 2343

Answers (2)

Christoph Fink
Christoph Fink

Reputation: 23093

If you load it with HTTP(S) you can use WebClient.DownloadFileAsync which has all you need:

WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += (s, args) =>
{
    //args.BytesReceived, args.TotalBytesToReceive and args.ProgressPercentage will have all you need
};
webClient.DownloadFileCompleted += (s, args) =>
{
    //done
};
webClient.DownloadFileAsync(new Uri(remoteFile), localFile);
// or the following if you want it in memory:
webClient.DownloadStringCompleted += (s, args) => 
{
    //done, see args.Result for the string
};
webClient.DownloadStringAsync(new Uri(remoteFile));

If you need credentials to acces the page, you can use the Credentials property of the WebClient.

According to the notes on WebClient:

By default, the .NET Framework supports URIs that begin with http:, https:, ftp:, and file: scheme identifiers.

So it would even work with loading the file from a share using the file: identifier.

Upvotes: 3

Ali Khalid
Ali Khalid

Reputation: 1345

In order to find out the length (size) of the file before downloading, you have to read the Content-Length header in the HTTP response, it will provide the size of the file.

Source: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Upvotes: 0

Related Questions