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

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
icon_topic_posted.gif9/23/2003 2:20:01 AM
  #1 Re: Download status(webclient) 
 Reply  w/Quote  B'mark-It!  Edit  Del 
useless
(Addicted Member)
blank.gif
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

 


http://www.vbcity.com/forums/topic.asp?tid=41354
icon_topic_posted.gif7/13/2004 12:23:21 PM
  #2 Re: Download status(webclient) 
 Reply  w/Quote  B'mark-It!  Edit  Del 
VBGOD
(New Member)
blank.gif
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


icon_topic_posted.gif7/13/2004 11:39:54 PM
  #3 Re: [Resolved]Download status(webclient) 
 Reply  w/Quote  B'mark-It!  Edit  Del 
VBGOD
(New Member)
blank.gif
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;
            }
        }


icon_topic_posted.gif3/23/2005 3:49:31 AM
  #4 Re: [Resolved]Download status(webclient) 
 Reply  w/Quote  B'mark-It!  Edit  Del 
riico
(New Member)
blank.gif
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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值