|
Hi All,
I am developing an application With VB.NET 2.0 for downloaded files from the internet using the URL of the file.
I have already developed this and was working fine until i got the file more than 20MB.
the code is working perfectly for the small sized files.
Public Shared Function DownloadFile(ByVal remoteFilename As String, ByVal localFilename As String) As Integer
Dim bytesProcessed As Integer = 0
Dim remoteStream As Stream = Nothing
Dim localStream As Stream = Nothing
Dim response As WebResponse = Nothing
Try
Dim request As WebRequest = WebRequest.Create(remoteFilename)
request.Timeout = 5 * 60 * 1000
If request IsNot Nothing Then
response = request.GetResponse()
If response IsNot Nothing Then
remoteStream = response.GetResponseStream()
localStream = File.Create(localFilename)
Dim buffer As Byte() = New Byte(1023) {}
Dim bytesRead As Integer
Do
bytesRead = remoteStream.Read(buffer, 0, buffer.Length)
localStream.Write(buffer, 0, bytesRead)
bytesProcessed += bytesRead
Loop While bytesRead > 0
End If
End If
Catch e As Exception
bytesProcessed = -1
Console.WriteLine(e.Message)
Finally
If response IsNot Nothing Then
response.Close()
End If
If remoteStream IsNot Nothing Then
remoteStream.Close()
End If
If localStream IsNot Nothing Then
localStream.Close()
End If
End Try
' Return total bytes processed to caller.
Return bytesProcessed
End Function
Thanks In advance
|