Click here to Skip to main content
15,896,154 members

VB.NET Download a File from FTP Server Error

hansoctantan asked:

Open original thread
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
VB
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 'If user abort download
        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()
Tags: Visual Basic, Errors, FTP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900