renjithmadhavan
New member
- Joined
- Nov 24, 2010
- Messages
- 3
- Programming Experience
- Beginner
I am very new to VB.net . I am trying to use system.net to download a zip from a website . I am using the following code .
I am doing something wrong here . The zip file is getting downloaded but the size is not correct . How to know the size of the file I download and assign to my buffer . Is there any other way to do this . Please help ..
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filename As String = TextBox1.Text
Dim dest As String = "C:\test.zip"
Dim name As String = "http://www.Source URL/test.zip"
Dim wr As HttpWebRequest = CType(WebRequest.Create(name), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
Dim str As Stream = ws.GetResponseStream()
[B] Dim a As Integer = ws.ContentLength
Dim inBuf(a) As Byte[/B]
Dim bytesToRead As Integer = CInt(inBuf.Length)
Dim bytesRead As Integer = 0
While bytesToRead > 0
Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
If n = 0 Then
Exit While
End If
bytesRead += n
bytesToRead -= n
End While
Dim fstr As New FileStream(dest, FileMode.OpenOrCreate, FileAccess.Write)
fstr.Write(inBuf, 0, bytesRead)
str.Close()
fstr.Close()
MessageBox.Show("Downloaded Successfully")
End Sub
I am doing something wrong here . The zip file is getting downloaded but the size is not correct . How to know the size of the file I download and assign to my buffer . Is there any other way to do this . Please help ..