|
-
Jul 14th, 2005, 12:31 PM
#1
Thread Starter
Hyperactive Member
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:
Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
Const PACKET_SIZE As Integer = 1024 ^ 2
Dim ch As Char()
ReDim ch(PACKET_SIZE)
'Create the webclient and open a download stream
Dim WC As New Net.WebClient
Dim sr As New IO.StreamReader(WC.OpenRead(txtSrc.Text))
Dim sw As New IO.StreamWriter(txtDest.Text, False, System.Text.Encoding.Default)
Dim NumRead As Integer
'Download the file
lblProg.Text = "Downloading File..." : Application.DoEvents()
While sr.Peek >= 0
NumRead = sr.ReadBlock(ch, 0, PACKET_SIZE)
sw.Write(ch, 0, NumRead)
End While
'Tidy up
sw.Close() : sw = Nothing
sr.Close() : sr = Nothing
WC.Dispose() : WC = Nothing
lblProg.Text = "Operation Complete"
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
-
Jul 15th, 2005, 02:58 AM
#2
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|