Results 1 to 3 of 3

Thread: System.Net.Webclient

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95

    System.Net.Webclient

    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:
    1. Public Sub DownloadFile(ByVal fileUrl As String, ByVal saveTo As String, ByVal progress As ProgressBar)
    2.  
    3.         Dim client As New WebClient
    4.         Dim myStream As Stream = client.OpenRead(fileUrl)
    5.  
    6.         progress.Visible = True
    7.         progress.Maximum = CType(myStream.Length, Integer)
    8.  
    9.         'Dim sr As New StreamReader(myStream)
    10.  
    11.         Dim r As New BinaryReader(myStream)
    12.         Dim fs As New FileStream(saveTo, FileMode.Create)
    13.         Dim w As New BinaryWriter(fs)
    14.  
    15.         Dim i As Long, res As Byte, locate As Integer = 0
    16.  
    17.         For i = 0 To myStream.Length
    18.  
    19.             res = r.ReadByte()
    20.             w.Write(res)
    21.             progress.Value += 1
    22.  
    23.         Next i
    24.  
    25.         progress.Visible = False
    26.         progress.Value = 0
    27.  
    28.         w.Close()
    29.         fs.Close()
    30.         r.Close()
    31.  
    32.        
    33.     End Sub

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    try this thread in the code bank ... retrieve image size+download with progressbar ( from the web )
    it's a sample i made for downloading images with a progressbar , but can be used / modified for downloading files also.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95
    Thanks for the reply, that did the trick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width