[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net download file

Posted on 2009-02-18
5
Medium Priority
?
283 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:tonelm54
  • 3
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 23680921
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 23681604
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
 

Author Comment

by:tonelm54
ID: 23689695
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
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 23692226
>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
 
LVL 38

Expert Comment

by:PaulHews
ID: 23692247
>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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question