VB.net download file

Ive written a simple program which downloads a file from the internet, which works great.

Problem is, it downloads the file corrupt, and dont understand where its going wrong.

Any ideas would be great.

Thanks in advance
    Sub dwnFile(ByVal strURL As String, ByVal strFile As String)
        Dim httpReq As System.Net.HttpWebRequest
        Dim httpResp As System.Net.HttpWebResponse
 
        Dim httpURL As New System.Uri(strURL)
        httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
        httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
 
        Dim str As Stream = httpResp.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0
            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
        End While
 
        Dim fstr As New FileStream(strFile, FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
    End Sub

Open in new window

tonelm54Asked:
Who is Participating?
 
PaulHewsCommented:
>if the array element was empty try again that loop

If the array element is empty that's a signal that your file has finished... That's the most reliable way to tell that your download is actually finished.

>but the file seems to die half way through

Presumably you get some trappable exception?  If so, then you can use httpwebrequest.addrange to request the part of the file that was not downloaded.  See the docs here, and note the addition at the bottom about checking the status code.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.addrange.aspx
0
 
CodeCruiserCommented:
You may be interested in the following method
      My.Computer.Network.DownloadFile(filepath/url,destinationfile)

Here is full description
http://msdn.microsoft.com/en-us/library/ack30t8y(VS.80).aspx
0
 
PaulHewsCommented:
The problem with your code is that it doesn't write out the bytes as they are put in the buffer during the loop, so only the last 100001 bytes of the file are written to the file.  Here's how to fix:
Public Sub dwnFile(ByVal strURL As String, ByVal strFile As String)
        Dim httpReq As System.Net.HttpWebRequest
        Dim httpResp As System.Net.HttpWebResponse
 
        Dim httpURL As New System.Uri(strURL)
        httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
        httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
 
        Dim str As IO.Stream = httpResp.GetResponseStream()
        Dim inBuf(100000) As Byte
        Dim fstr As New IO.FileStream(strFile, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
        Dim n As Integer
        Do
            n = str.Read(inBuf, 0, inBuf.Length)
            If n = 0 Then
                Exit Do
            End If
            fstr.Write(inBuf, 0, n)
        Loop
        str.Close()
        fstr.Close()
    End Sub

Open in new window

0
 
tonelm54Author Commented:
Ok, dont think this is actually going to work well now :-(

The original idea was to download a file, but the file seems to die half way through, and I was looking for a way to resume the download from where it left off, ie download each loop to an array, if the array element was empty try again that loop, until everything is completed, but I dont think this code will do that will it? Feel free to hopefully correct me.
0
 
PaulHewsCommented:
>If the array element is empty that's a signal that your file has finished...

More accurately, the bytes read returned from the read function tells you when you are finished.

>n = str.Read(inBuf, 0, inBuf.Length)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.