dynamic_sysop
Well-known member
hi dudes, another little project i knocked up for fun . this will allow you to get the size of a file on the net , then download it with a progressbar to indicate how its going.
i built one with SavefileDialog option also, but to reduce the code being posted this will save a default image name to your apps bin folder.
ok , first you need a reference to these 2 at the top of your form ...
then a command button and a progressbar ( Button1 and Progressbar1 ) then drop this code in ...
ive included a sample project ...
i built one with SavefileDialog option also, but to reduce the code being posted this will save a default image name to your apps bin folder.
ok , first you need a reference to these 2 at the top of your form ...
Code:
Imports System.Net
Imports System.IO
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim imgPath As String = "http://www.google.com/images/logo.gif"
Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(imgPath), HttpWebRequest)
Dim wResponse As WebResponse = DirectCast(wRequest.GetResponse(), WebResponse)
Dim sWriter As FileStream = New FileStream("new.gif", FileMode.OpenOrCreate)
Dim ContentLength As Integer = wResponse.ContentLength
Dim sizeInKB As String = (ContentLength / 1024).ToString()
Me.Text = "The Size of the Image is: " & sizeInKB.Substring(0, sizeInKB.IndexOf(".") + 3) & "KB"
Dim buffer(ContentLength) As Byte
Dim length As Integer = ContentLength
Dim position As Integer = 0
Dim complete As Integer = 1
Dim returned As Integer = 0
ProgressBar1.Value = 0 /// clear the progressbar.
ProgressBar1.Maximum = ContentLength /// set its length.
While Not complete = 0
position = wResponse.GetResponseStream().Read(buffer, returned, length)
sWriter.Write(buffer, returned, position)
complete = position
returned += position
length -= position
ProgressBar1.Step = returned
ProgressBar1.PerformStep()
End While
Me.Text = "Completed download"
sWriter.Close()
wRequest = Nothing
Catch ex As Exception
Console.WriteLine(ex.Message)
Catch webex As WebException
Console.WriteLine(webex.Message)
End Try
End Sub