[RESOLVED] Downloading files
I'm trying to write a windows app that will download files from a server. It is working fine for small files, but not the larger ones (50Mb+)- I get the error "An operation on a socket could not be performed because the system lacked
sufficient buffer space or because a queue was full."
This is the code I am using:
Code:
Private Function DownloadFile(ByVal fileSource As String, _
ByVal fileDestination As String, _
ByVal WebRequestType As String) As Boolean
Dim myHttpWebRequest As HttpWebRequest
Dim myHttpWebResponse As HttpWebResponse
Dim myFileWebRequest As FileWebRequest
Dim myFileWebResponse As FileWebResponse
Dim myStream As Stream
Dim bufferSize As Integer
Dim inBuf() As Byte
Dim bytesToRead As Integer
Dim bytesRead As Integer = 0
Dim myFileStream As FileStream
Dim n As Integer
Dim folderDestination As String
'Dim PercentageComplete As Integer
Dim clsUtils As New clsUtilities
Try
If WebRequestType = "HTTP" Then
myHttpWebRequest = CType(WebRequest.Create(fileSource), HttpWebRequest)
myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
myStream = myHttpWebResponse.GetResponseStream()
bufferSize = myHttpWebResponse.ContentLength
Else 'File
myFileWebRequest = CType(WebRequest.Create(fileSource), FileWebRequest)
myFileWebResponse = CType(myFileWebRequest.GetResponse(), FileWebResponse)
myStream = myFileWebResponse.GetResponseStream()
bufferSize = myFileWebResponse.ContentLength
End If
'MsgBox("bufferSize=" & bufferSize)
'Set buffer size
ReDim inBuf(bufferSize)
bytesToRead = CInt(inBuf.Length)
'Set up progressbar
ResetLoadingPanel()
DownloadProgressBar.Maximum = bufferSize
While bytesToRead > 0
n = myStream.Read(inBuf, bytesRead, bytesToRead)
If n = 0 Then
Exit While
End If
bytesRead += n
bytesToRead -= n
'Update progress bar
DownloadProgressBar.Step = n
DownloadProgressBar.PerformStep()
'Update Loading Panel
UpdateLoadingPanel(DownloadProgressBar.Value, DownloadProgressBar.Maximum)
End While
'Remove existing files
If Not clsUtils.FileExists(fileDestination) Then
WriteToInstallInfo("File already exists. Deleting...", True, True)
File.Delete(fileDestination)
WriteToInstallInfo("...deleted ", False, False)
End If
'Create folder if required
folderDestination = clsUtils.FolderFromFileName(fileDestination)
If Not clsUtils.FolderExists(folderDestination) Then
WriteToInstallInfo("Creating folder '" & folderDestination & "' ... ", True, True)
Directory.CreateDirectory(folderDestination)
WriteToInstallInfo("... created ", False, False)
End If
myFileStream = New FileStream(fileDestination, FileMode.OpenOrCreate, FileAccess.Write)
myFileStream.Write(inBuf, 0, bytesRead)
myStream.Close()
myFileStream.Close()
WriteToInstallInfo("...download successful.", True, True)
Return True
Catch ex As Exception
WriteToInstallInfo("**ERROR** DownloadFile:" & ex.Message, True, True)
Return False
End Try
End Function
If I can modify this method to work, then great. Otherwise, any ideas as to what I should be doing?