Download Files from File Server

Naimishforu
Posted by Naimishforu under VB.NET category on | Points: 40 | Views : 2697
  Private Sub DownloadFile(ByVal DownloadLocation As String, ByVal FileName As String)
Dim wr As FileWebRequest = CType(WebRequest.Create(DownloadLocation), FileWebRequest)
Dim ws As FileWebResponse = CType(wr.GetResponse(), FileWebResponse)
Dim str As Stream = ws.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(FileName, FileMode.OpenOrCreate, FileAccess.Write)
fstr.Write(inBuf, 0, bytesRead)
str.Close()
fstr.Close()
End Sub

Comments or Responses

Login to post response