Results 1 to 2 of 2

Thread: Downloading files using Webclient [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Resolved Downloading files using Webclient [RESOLVED]

    I'm attempting to use a Webclient to download files from the internet, but I am running into problems. So far I have created a simple test project to get it working correctly, the code is below:

    VB Code:
    1. Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
    2.         Const PACKET_SIZE As Integer = 1024 ^ 2
    3.         Dim ch As Char()
    4.         ReDim ch(PACKET_SIZE)
    5.  
    6.         'Create the webclient and open a download stream
    7.         Dim WC As New Net.WebClient
    8.         Dim sr As New IO.StreamReader(WC.OpenRead(txtSrc.Text))
    9.         Dim sw As New IO.StreamWriter(txtDest.Text, False, System.Text.Encoding.Default)
    10.         Dim NumRead As Integer
    11.  
    12.         'Download the file
    13.         lblProg.Text = "Downloading File..." : Application.DoEvents()
    14.         While sr.Peek >= 0
    15.             NumRead = sr.ReadBlock(ch, 0, PACKET_SIZE)
    16.             sw.Write(ch, 0, NumRead)
    17.         End While
    18.  
    19.         'Tidy up
    20.         sw.Close() : sw = Nothing
    21.         sr.Close() : sr = Nothing
    22.         WC.Dispose() : WC = Nothing
    23.  
    24.         lblProg.Text = "Operation Complete"
    25.     End Sub

    However, the problem that I'm having is that the webclient doesn't always download a complete file. It will download text files without a hitch, but when you try and use it on something like a zip or executable it will download only a fraction of the file - usually about two thirds.

    I have a suspicion the problem lies with the StreamWriter - could it be that the streamwriter is not writing the non-printable characters, hence why it works on text files correctly?
    Last edited by Parallax; Jul 15th, 2005 at 02:59 AM. Reason: Resolved

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Re: Downloading files using Webclient

    Ah, never mind. I've got it working now by using a BinaryReader/BinaryWriter instead - it was the damn encoding of the streamreader causing problems.

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