What's the proper way to download a file off the Internet? I could use WebRequest or WebClient, which one am I better off using?
I want to build a progress bar for my downloader, so how can I do that? I tried using webclient.OpenRead but the stream I got back I couldn't seek through, so how the hell do I know how big a file off the Internet is supposed to be?
Can't really implement a progress bar until I know that now, can I?
Here's my crappy ass code that doesn't work.
VB Code:
Public Sub DownloadFile(ByVal fileUrl As String, ByVal saveTo As String, ByVal progress As ProgressBar) Dim client As New WebClient Dim myStream As Stream = client.OpenRead(fileUrl) progress.Visible = True progress.Maximum = CType(myStream.Length, Integer) 'Dim sr As New StreamReader(myStream) Dim r As New BinaryReader(myStream) Dim fs As New FileStream(saveTo, FileMode.Create) Dim w As New BinaryWriter(fs) Dim i As Long, res As Byte, locate As Integer = 0 For i = 0 To myStream.Length res = r.ReadByte() w.Write(res) progress.Value += 1 Next i progress.Visible = False progress.Value = 0 w.Close() fs.Close() r.Close() End Sub




Reply With Quote