hey guys, I created a program that downloads a file from FTP Server
It reads the current date from FTP and download it if its not found in download directory. It runs fine when the program downloads a small file(5 MB below - this is the size of a file that I've seen in download directory) but when the program downloads a file more than 10 MB a runtime error will occur. Don't know whats wrong?
This is the Error
"The underlying connection was closed: An unexpected error occurred on a receive."
This is the code for downloading a file
Dim request As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing
request = DirectCast(FtpWebRequest.Create(New Uri(ftp)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.UseBinary = True
request.Credentials = New NetworkCredential(username, password)
response = DirectCast(request.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
Dim downloaded As Long = 0
Dim elapseTime As New Stopwatch
elapseTime.Start()
timeStarted = Format(Now, "hh:mm:ss tt")
outputStream = New FileStream(saveLocation + fileName, FileMode.Create)
fileCreated = True
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
If bckDownloader(index).CancellationPending Then
outputStream.Close()
File.Delete(saveLocation + fileName)
End If
downloaded = downloaded + readCount
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
If CInt(downloaded / 1024) - 2 = CInt((fileSize / 1024)) Then
Dim x As Integer = 0
End If
Me.Invoke(safeDownloadStatus, fileName, elapseTime.ElapsedMilliseconds, downloaded, fileSize, 1, index)
End While
Me.Invoke(safeChangeStatusStrip, 1, "")
Me.Invoke(safeLogs, fileName + " has been successfully downloaded")
Me.Invoke(safeDownloadStatus, "", 0, 0, 0, 3, index)
timeEnded = Format(Now, "hh:mm:ss tt")
Dim copyTo As String = downloadPath & dateStr & "\"
If Not Directory.Exists(copyTo) Then Directory.CreateDirectory(copyTo)
Dim logFile As String = copyTo & Format(Now, "hhmmss tt") & Mid(fi.Name, 18, Len(fi.Name))
logFile = Mid(logFile, 1, Len(logFile) - 4) & ".ftl"
WriteFileToLogs(logFile, jobInfo, fileName, dateStr, xlPath, imgCount, fileSize, saveTo & dateStr & "\", dateNow, timeStarted, timeEnded, "File successfully downloaded.", "0")
elapseTime.Stop()
objStreamReader.Close()