bool DownloadFile(string sURL, ProgressBar pProgress, string Filename)(文件下载,进度条)

http://www.vbcity.com/forums/topic.asp?tid=41354
Hi all

I want to know how can I get the size of the file being downloaded and the progress status so I can put it in a progress bar.


Code:

       Dim web As New System.Net.WebClient()
        web.DownloadFile(" http://www.somethig.com", CurDir() & "\file.exe")
 


If someone knows I would be very thankfull

Thx in advanced..


Edited by - useless on 9/25/2003 1:04:22 PM
9/23/2003 2:20:01 AM
   #1 Re: Download status(webclient)  
 Reply   w/Quote   B'mark-It!   Edit   Del 
useless
(Addicted Member)

Show this authors profile  Email the author of this post

posts: 106
since: Aug 31, 2003
from: Iceland
This reply is rated 1 point.
Well after hrs of search I found the answer and here it is for you all..


Code:

Function DownloadChunks(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String)
        Dim wRemote As System.Net.WebRequest
        Dim URLReq As HttpWebRequest
        Dim URLRes As HttpWebResponse
        Dim FileStreamer As New FileStream(Filename, FileMode.Create)
        Dim bBuffer(999) As Byte
        Dim iBytesRead As Integer

        Try
            URLReq = WebRequest.Create(sURL)
            URLRes = URLReq.GetResponse
            Dim sChunks As Stream = URLReq.GetResponse.GetResponseStream
            pProgress.Maximum = URLRes.ContentLength

            Do
                iBytesRead = sChunks.Read(bBuffer, 0, 1000)
                FileStreamer.Write(bBuffer, 0, iBytesRead)
                If pProgress.Value + iBytesRead <= pProgress.Maximum Then
                    pProgress.Value += iBytesRead
                Else
                    pProgress.Value = pProgress.Maximum
                End If
            Loop Until iBytesRead = 0
            pProgress.Value = pProgress.Maximum
            sChunks.Close()
            FileStreamer.Close()
            Return sResponseData
        Catch
            MsgBox(Err.Description)
        End Try
    End Function

 


7/13/2004 12:23:21 PM
   #2 Re: Download status(webclient)  
 Reply   w/Quote   B'mark-It!   Edit   Del 
VBGOD
(New Member)

Show this authors profile  Email the author of this post  Visit the authors home page

posts: 2
since: Jul 13, 2004
from: USA
This reply is rated 3 points.

Quote:
Posted by useless on 9/23/2003 2:20:01 AM (PST):

Well after hrs of search I found the answer and here it is for you all..


Code:

Function DownloadChunks(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String)
        Dim wRemote As System.Net.WebRequest
        Dim URLReq As HttpWebRequest
        Dim URLRes As HttpWebResponse
        Dim FileStreamer As New FileStream(Filename, FileMode.Create)
        Dim bBuffer(999) As Byte
        Dim iBytesRead As Integer

        Try
            URLReq = WebRequest.Create(sURL)
            URLRes = URLReq.GetResponse
            Dim sChunks As Stream = URLReq.GetResponse.GetResponseStream
            pProgress.Maximum = URLRes.ContentLength

            Do
                iBytesRead = sChunks.Read(bBuffer, 0, 1000)
                FileStreamer.Write(bBuffer, 0, iBytesRead)
                If pProgress.Value + iBytesRead <= pProgress.Maximum Then
                    pProgress.Value += iBytesRead
                Else
                    pProgress.Value = pProgress.Maximum
                End If
            Loop Until iBytesRead = 0
            pProgress.Value = pProgress.Maximum
            sChunks.Close()
            FileStreamer.Close()
            Return sResponseData
        Catch
            MsgBox(Err.Description)
        End Try
    End Function

 


Your VB.NET code is pure genius

It's the only one that seems to work, but... do you also know anything about C#?

I was trying to convert that VB code to C#, but I've ran into one problem.
 :-/

Herb


7/13/2004 11:39:54 PM
   #3 Re: [Resolved]Download status(webclient)  
 Reply   w/Quote   B'mark-It!   Edit   Del 
VBGOD
(New Member)

Show this authors profile  Email the author of this post  Visit the authors home page

posts: 2
since: Jul 13, 2004
from: USA
This reply is rated 3 points.
It's a lot different in C#


Code:

        private bool DownloadFile(string sURL, ProgressBar pProgress, string Filename)
        {
            System.Net.HttpWebRequest URLReq;
            System.Net.HttpWebResponse URLRes;
            System.IO.FileStream FileStreamer;
            byte[] bBuffer = new byte[999];
            int iBytesRead = 0;

            try
            {
                FileStreamer = new FileStream(Filename, System.IO.FileMode.Create);
                URLReq = (HttpWebRequest)System.Net.WebRequest.Create(sURL);
                URLRes = (HttpWebResponse)URLReq.GetResponse();
                Stream sChunks = URLReq.GetResponse().GetResponseStream();
                pProgress.Maximum = Convert.ToInt32(URLRes.ContentLength);
                pProgress.Visible = false;

                do
                {
                    iBytesRead = sChunks.Read(bBuffer, 0, 1000);
                    FileStreamer.Write(bBuffer, 0, iBytesRead);

                    if (pProgress.Value + iBytesRead <= pProgress.Maximum)
                    {
                        pProgress.Value += iBytesRead;
                    }
                    else
                    {
                        pProgress.Value = pProgress.Maximum;
                    }
                }
                while (iBytesRead != 0);

            pProgress.Value = pProgress.Maximum;
            sChunks.Close();
            FileStreamer.Close();
            return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return false;
            }
        }


3/23/2005 3:49:31 AM
   #4 Re: [Resolved]Download status(webclient)  
 Reply   w/Quote   B'mark-It!   Edit   Del 
riico
(New Member)

Show this authors profile  Email the author of this post

posts: 1
since: Mar 23, 2005
This reply is rated 3 points.
Hi all,
i have noticed that the script doesn't work with small files. It works only with files with a lot of megabytes.

Somebody can help me to modify the code for run it also on small file? I work on c#

Thanks a lot!

你可能感兴趣的:(ProgressBar)