SciLor's Open Source Forums
http://forum.scilor.com/

Get Downloaded Percentage
http://forum.scilor.com/viewtopic.php?f=133&t=324
Page 1 of 1

Author:  thatsgreat2345 [ Mon Sep 13, 2010 6:34 am ]
Post subject:  Get Downloaded Percentage

I was wondering how you got the size of the file you are downloading, and thus were allowed to do the progress bar
I've got the song successfully downloaded, just can't seem to find out how to get the size of the song
Here is a small piece of code that does the writing to the mp3 file
(fs is the filestream, and buffer was created before)

Thanks for any help, couldn't have gotten to where I am with out many of the small snippets posted on the site in various places

Code:
            Stream stream = response.GetResponseStream();
           

            int count = 0;
            do
            {
                count = stream.Read(buffer, 0, buffer.Length);
                if (count > 0)
                {
                    fs.Write(buffer, 0, count);
                }
            } while (count > 0);




EDIT: I attempted to use Length instead of ContentLength on the responsestream, stupid me!!

Author:  SciLor [ Mon Sep 13, 2010 4:30 pm ]
Post subject:  Re: Get Downloaded Percentage

Yeah, ContentLength gives you all that is needed!

Author:  thatsgreat2345 [ Mon Sep 13, 2010 10:32 pm ]
Post subject:  Re: Get Downloaded Percentage

I guess I'll post the solution in case others read this

Code:
long clength =response.ContentLength;

            Stream stream = response.GetResponseStream();
           

            int count = 0;
            long dw = 0;
            do
            {
                count = stream.Read(buffer, 0, buffer.Length);
                dw += count;
                Console.WriteLine("Dw: {0} CLength:{1} = {2}", dw, clength, (dw * 100 / clength));
                if (count > 0)
                {
                    os.Write(buffer, 0, count);
                }
            } while (count > 0);

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/