download file

dpan

Active member
Joined
Nov 22, 2005
Messages
28
Programming Experience
Beginner
I am having a problem and I do not know why. I am tring to download a file while adding download progress to my progress bar. It works fine for most files, however if I try to download a file with a .php extention I get an error.

Here is the code:
VB.NET:
Expand Collapse Copy
        Dim request As WebRequest
        Dim response As WebResponse
        Dim reader As Stream
        Dim writer As Stream
        Dim data(10) As Byte
        Dim count As Integer
        Dim total As Integer
        Me.Show()
        Me.Text = "Downloading file ....."
        Application.DoEvents()
        request = WebRequest.Create("http://www.magoffin.k12.ky.us/docidregistration.php")
        response = request.GetResponse()
        reader = response.GetResponseStream()

        ProgressBar1.Maximum = CInt(response.ContentLength)
        ProgressBar1.Value = 0
        total = 0
        writer = File.Create("test.htm")
        While True
            count = reader.Read(data, 0, 11)
            If count <= 0 Then
                Exit While
            End If
            writer.Write(data, 0, count)
            total += 11
            If total < ProgressBar1.Maximum Then ProgressBar1.Value = total
            Application.DoEvents()
        End While

        reader.Close()
        writer.Close()
        Application.DoEvents()
        Me.Text = "all done"

her is my error:
VB.NET:
Expand Collapse Copy
System.ArgumentOutOfRangeException was unhandled
  Message="Value of '-1' is not valid for 'Maximum'. 'Maximum' must be greater than or equal to 0.
Parameter name: Maximum"..................

any idea why this giving an error....
 
"The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1."

From the error you are getting, I would say the above applies to:

ProgressBar1.Maximum = CInt(response.ContentLength)

HTH

Blokz
 
Back
Top