|
-
Jul 20th, 2010, 09:36 AM
#1
Thread Starter
Member
[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?
-
Jul 21st, 2010, 05:04 AM
#2
Thread Starter
Member
Re: Downloading files
Found this method, which works great:
Code:
Function DownloadChunks(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String)
Dim wRemote As System.Net.WebRequest
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse
Dim FileStreamer As New FileStream(Filename, FileMode.Create)
Dim bBuffer(999) As Byte
Dim iBytesRead As Integer
Try
URLReq = WebRequest.Create(sURL)
URLRes = URLReq.GetResponse
Dim sChunks As Stream = URLReq.GetResponse.GetResponseStream
pProgress.Maximum = URLRes.ContentLength
Do
iBytesRead = sChunks.Read(bBuffer, 0, 1000)
FileStreamer.Write(bBuffer, 0, iBytesRead)
If pProgress.Value + iBytesRead <= pProgress.Maximum Then
pProgress.Value += iBytesRead
Else
pProgress.Value = pProgress.Maximum
End If
Loop Until iBytesRead = 0
pProgress.Value = pProgress.Maximum
sChunks.Close()
FileStreamer.Close()
Return sResponseData
Catch
MsgBox(Err.Description)
End Try
End Function
Presumably it works because it limits the size of the buffer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|