Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

show download progress of file

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
GB
Hi, I have used the code from FAQ: faq796-3118 which works a treat, but I would like to show the download progress of the file in question using a progress bar.
Code:
    Public Sub GetPackage(ByVal sFile As String, ByVal destination As String, ByVal sRemoteUrl As String)
        Dim wc As New Net.WebClient()
        Try
            wc.DownloadFile(sRemoteUrl + sFile, destination + "\\" + sFile)
        Catch
            MsgBox("Unable to download file " + sRemoteUrl + sFile)
        End Try
    End Sub
Is this possible??

Any ideas?

Regards,

Martin

Computing Help And Info:
 
Well, here's a pretty decent article on how to do it in C#. It shouldn't be too difficult to convert to VB.NET.

Looks like you're going to need to create an HttpWebRequest and HttpWebResponse and loop through the stream to get the size of the file and then update the progress bar.

Ron Wheeler
 
I'm not a high level vb.net coder and having difficulty in translating it...

I'm not sure on how to do convert the C# vars to vb

anyone got any pointers or show me a bit?



Regards,

Martin

Computing Design And Services:
 
I have tried the code (project) posted by ninjadeathmonkey but it stucks at 7%
 
Here is a set of functions I have used for a VB 2003 project I had doing something similar.

*Note* pBar is only an object because I was using a custom progress bar - otherwise I would have made it of type ProgressBar.

Code:
    Private Sub SetBar(ByRef pBar As Object, ByVal Max As Integer)
        If Not pBar Is Nothing Then
            'Only do this if there is a TotalProgressBar
            pBar.Minimum = 0
            pBar.Value = 0
            pBar.Maximum = Max
        End If
    End Sub
    Private Function DownloadProgress(ByVal URL As String, ByVal Local As String, ByRef pBar As Object) As Boolean
        Try
            'Setup the WebRequest
            Dim wRemote As System.Net.WebRequest
            'The file buffer
            Dim bBuffer(999) As Byte
            'Number of bytes read
            Dim iBytesRead As Integer
            'Create the web request to the file - url
            wRemote = System.Net.WebRequest.Create(URL)
            'set the file to write to
            Dim Loc As String = Local
            'set the file stream for writing
            Dim sw As System.IO.FileStream = New System.IO.FileStream(Loc, IO.FileMode.Create, IO.FileAccess.Write)
            'get the response from the server
            Dim myWebResponse As System.Net.WebResponse = wRemote.GetResponse()
            SetBar(pBar, Convert.ToInt32(myWebResponse.ContentLength))
            'get the chunkcs from the web response
            Dim sChunks As System.IO.Stream = myWebResponse.GetResponseStream
            Do
                'read bytes in 256 byte intervals
                iBytesRead = sChunks.Read(bBuffer, 0, 256)
                If Not pBar Is Nothing Then
                    'if there is a progress bar
                    'increment it
                    If pBar.Value + iBytesRead <= pBar.Maximum Then
                        pBar.Value += iBytesRead
                    Else
                        pBar.Value = pBar.Maximum
                    End If
                End If
                'write the buffer to the file
                sw.Write(bBuffer, 0, iBytesRead)
            Loop While Not (iBytesRead = 0)
            'file is downloaded
            If Not pBar Is Nothing Then
                'if there is a progress bar, set to maximum
                pBar.Value = pBar.Maximum
            End If
            'close streams
            sChunks.Close()
            sw.Close()
            Return True
        Catch ex As Exception
            'display error
            MsgBox(ex.Message)
            Return False
        End Try
    End Function

Hope that helps you out.
 
What if you close the HttpWebResponse right after you get the ContentLength? Someone in the comments on that page pointed out that they kept timing out until they added that line.

(In VB.NET)
Code:
[green]' Set default authentication for retrieving the file[/green]
webRequest.Credentials = CredentialCache.DefaultCredentials
 
[green]' Retrieve the response from the server[/green]
webResponse = CType(webRequest.GetResponse(), HttpWebResponse)
 
[green]' Ask the server for the file size and store it[/green]
Dim fileSize As Int64 =  webResponse.ContentLength 
 
[b]webResponse.Close()[/b]
 
[green]' Open the URL for download[/green]
strResponse = wcDownload.OpenRead(txtURL.Text)

Ron Wheeler
Tekdev Open Source Development
 
The timing out could possibly be because you are opening two connections at one time. According to the article you are opening a connection (via WebRequest/WebResponse) to get the ContentLength. This connection is left open and then another connection is opened via the WebClient wcDownload.OpenRead() which provides the Stream used for getting the data. It makes sense then that adding the webResponse.Close() line after getting the ContentLength would cause that to work.

Rather than using two different objects to accomplish the goal you could also continue to use the WebResponse object and get the stream using the GetResponseStream() method. This method uses only one connection total. Just remember to close it after your done downloading the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top