.NET FTP Object doing CD's..???

B

Ben Kim

Hello all,

We are connecting to a remote server using FTP and sending up 100's of files
daily to this site. I just received a call from the operations manager of
this site and they are complaining that we are doing a CD.. in our
connection prior to uploading. Once we signon, they claim we are "in" the
correct folder. I have adopted this code from another programmer who is
long gone. I do not see anything in this code that would cause it to do a
CD.. once connected to the site and then do a CD into the appropriate
folder. Does the FTP object have a setting I can change that will get rid
of this behavior? We are calling the following class like:

Private _ftp As FtpClient
.....
_ftp = New Ftpclient(Me)
...._ftp.DoZipUpload()

Below is the code for the FtpClient class. Any ideas would be greatly
appreciated!
Imports System.Net

Imports System.IO

Public Class Ftpclient

Private Base As EtiBase

Sub New(ByRef aEtiBase As EtiBase)

Base = aEtiBase

End Sub

Public Sub DoZipUpload()

Base.LogToSummary("Uploading data.")

DoUpload(Base.Ini.outputzipfilename, Base.Ini.ftpuser,
Base.Ini.ftppasswordtext, Base.Ini.ftpserver, Base.Ini.ftppassive)

End Sub

Private Sub AttemptFtpConnect(ByVal username As String, ByVal password As
String, ByVal UsePassive As String, ByVal uploadUrl As String, ByRef
requestStream As Stream, ByRef uploadRequest As FtpWebRequest)

uploadRequest = Nothing

requestStream = Nothing

Try 'attempt connect

uploadRequest = WebRequest.Create(uploadUrl) 'set connection options

uploadRequest.Method = WebRequestMethods.Ftp.UploadFile

uploadRequest.Proxy = Nothing 'no proxy, no passive

uploadRequest.UsePassive = Convert.ToBoolean(UsePassive)

uploadRequest.Credentials = New NetworkCredential(username, password)

requestStream = uploadRequest.GetRequestStream()

Base.LogToSummary(vbCrLf & "FTP connect success.")

Catch ex As Exception

Base.LogToSummary(vbCrLf & "FTP connect failed.")

Base.LogToDebug(String.Format("exception! message:[{0}] source:[{1}]",
ex.Message, ex.Source))

End Try

End Sub

Private Sub DoUpload(ByVal fileName As String, ByVal username As String,
ByVal password As String, ByVal host As String, ByVal UsePassive As String)

Base.ParentForm.SetText("Attempting to connect to ftp...")

Base.LogToDebug(String.Format("ftp: host[{0}] user[{1}] password-length[{2}]
file[{3}]", host, username, password.Length, fileName))

Dim uploadUrl As String = "ftp://" & host & "/" & Path.GetFileName(fileName)

Dim requestStream As Stream = Nothing

Dim uploadRequest As FtpWebRequest = Nothing

'try to get ftp conenction

AttemptFtpConnect(username, password, UsePassive, uploadUrl, requestStream,
uploadRequest)

'retry if failed

If requestStream Is Nothing Then

Base.WaitForServer(host)

AttemptFtpConnect(username, password, UsePassive, uploadUrl, requestStream,
uploadRequest)

End If

'retry if failed

If requestStream Is Nothing Then

Base.WaitForServer(host)

AttemptFtpConnect(username, password, UsePassive, uploadUrl, requestStream,
uploadRequest)

End If

'if failed give up

If requestStream Is Nothing Then

Base.successorfail = "FAILURE"

Base.failreason = "Cannot connect to FTP server."

End If

'send file over connection

If Not requestStream Is Nothing Then

SendFile(fileName, requestStream, uploadRequest)

End If

Base.ParentForm.SetText("Upload was a " & Base.successorfail)

End Sub

Private Sub SendFile(ByRef filename As String, ByRef requestStream As
Stream, ByRef uploadRequest As FtpWebRequest)

Base.ParentForm.SetText("Uploading data. 0% done.")

Dim fileStream As FileStream = Nothing

'get stats so we can update pretty gui status

Dim f As New System.IO.FileInfo(filename)

Dim fileSize As Long = f.Length

f = Nothing

Dim lastPercent As Integer = 0

Dim totalSent As Long = 0

'read local file in chunks, copying to server

fileStream = File.Open(filename, FileMode.Open)

Dim buffer(1024) As Byte

Dim bytesRead As Integer

Try

While True

'read from file

bytesRead = fileStream.Read(buffer, 0, buffer.Length)

If bytesRead = 0 Then

Exit While

End If

'write to ftp stream

requestStream.Write(buffer, 0, bytesRead)

'log percent to gui if changing

totalSent = totalSent + bytesRead

lastPercent = System.Math.Truncate(100 * (totalSent / fileSize))

If lastPercent Mod System.Convert.ToInt16(Base.Ini.refreshgui) = 0 Then

Base.ParentForm.SetText(String.Format("Uploading data. {0:F0}% done.",
lastPercent))

End If

End While

Catch ex As Exception

Base.LogToSummary(vbCrLf & "Data upload failed before completion.")

Base.LogToDebug(String.Format("exception! message:[{0}] source:[{1}]",
ex.Message, ex.Source))

Base.successorfail = "FAILURE"

Base.failreason = "Data upload failed before completion."

End Try

requestStream.Close()

'make sure server took it, look for server to say complete

Dim uploadResponse As FtpWebResponse = Nothing

uploadResponse = uploadRequest.GetResponse()

If uploadResponse.StatusDescription.ToLower.Contains("transfer complete") Or
uploadResponse.StatusDescription.StartsWith("226") Then

Base.LogToSummary(vbCrLf & "Data upload successful.")

Base.successorfail = "SUCCESS"

Else

Base.LogToSummary(vbCrLf & "Data upload complete, but remote server failed
to confirm reception.")

Base.successorfail = "FAILURE"

Base.failreason = "Data upload complete, but remote server failed to confirm
reception."

End If

uploadResponse.Close()

requestStream.Close()

fileStream.Close()

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top