Click here to Skip to main content
13,501,050 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
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 'If user abort download
        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")

Posted 13-Feb-12 2:59am
Updated 15-Feb-12 21:46pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is a list of properties you may like to set to see if they help you:

request.Timeout = 10000
request.ReadWriteTimeout = 10000
request.KeepAlive = False
request.UseBinary = True

Not saying these are the right settings, just that they are settings you should play with. I think the longer your timeout, the better. UseBinary should be true. Not sure with KeepAlive, I think it has to do with sending dummy requests to keep a connection open, so it probably doesn't matter if you're using it right away on a big file. Can't hurt to try it though.
hansoctantan 14-Feb-12 2:01am
I tried this and same just happened...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 16 Feb 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100