tonelm54
asked on
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
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
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>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)
More accurately, the bytes read returned from the read function tells you when you are finished.
>n = str.Read(inBuf, 0, inBuf.Length)
My.Computer.Network.Downlo
Here is full description
http://msdn.microsoft.com/en-us/library/ack30t8y(VS.80).aspx