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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Kim

    #1

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

    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.DoZipU pload()

    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.LogToSumma ry("Uploading data.")

    DoUpload(Base.I ni.outputzipfil ename, Base.Ini.ftpuse r,
    Base.Ini.ftppas swordtext, Base.Ini.ftpser ver, Base.Ini.ftppas sive)

    End Sub

    Private Sub AttemptFtpConne ct(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.Crea te(uploadUrl) 'set connection options

    uploadRequest.M ethod = WebRequestMetho ds.Ftp.UploadFi le

    uploadRequest.P roxy = Nothing 'no proxy, no passive

    uploadRequest.U sePassive = Convert.ToBoole an(UsePassive)

    uploadRequest.C redentials = New NetworkCredenti al(username, password)

    requestStream = uploadRequest.G etRequestStream ()

    Base.LogToSumma ry(vbCrLf & "FTP connect success.")

    Catch ex As Exception

    Base.LogToSumma ry(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("Attem pting 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.GetFileNam e(fileName)

    Dim requestStream As Stream = Nothing

    Dim uploadRequest As FtpWebRequest = Nothing

    'try to get ftp conenction

    AttemptFtpConne ct(username, password, UsePassive, uploadUrl, requestStream,
    uploadRequest)

    'retry if failed

    If requestStream Is Nothing Then

    Base.WaitForSer ver(host)

    AttemptFtpConne ct(username, password, UsePassive, uploadUrl, requestStream,
    uploadRequest)

    End If

    'retry if failed

    If requestStream Is Nothing Then

    Base.WaitForSer ver(host)

    AttemptFtpConne ct(username, password, UsePassive, uploadUrl, requestStream,
    uploadRequest)

    End If

    'if failed give up

    If requestStream Is Nothing Then

    Base.successorf ail = "FAILURE"

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

    End If

    'send file over connection

    If Not requestStream Is Nothing Then

    SendFile(fileNa me, requestStream, uploadRequest)

    End If

    Base.ParentForm .SetText("Uploa d was a " & Base.successorf ail)

    End Sub

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

    Base.ParentForm .SetText("Uploa ding data. 0% done.")

    Dim fileStream As FileStream = Nothing

    'get stats so we can update pretty gui status

    Dim f As New System.IO.FileI nfo(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(filen ame, 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.W rite(buffer, 0, bytesRead)

    'log percent to gui if changing

    totalSent = totalSent + bytesRead

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

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

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

    End If

    End While

    Catch ex As Exception

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

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

    Base.successorf ail = "FAILURE"

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

    End Try

    requestStream.C lose()

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

    Dim uploadResponse As FtpWebResponse = Nothing

    uploadResponse = uploadRequest.G etResponse()

    If uploadResponse. StatusDescripti on.ToLower.Cont ains("transfer complete") Or
    uploadResponse. StatusDescripti on.StartsWith(" 226") Then

    Base.LogToSumma ry(vbCrLf & "Data upload successful.")

    Base.successorf ail = "SUCCESS"

    Else

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

    Base.successorf ail = "FAILURE"

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

    End If

    uploadResponse. Close()

    requestStream.C lose()

    fileStream.Clos e()

    End Sub

    End Class


  • Ben Kim

    #2
    Re: .NET FTP Object doing CD's..???

    No takers? Is there a different group I should post this message to?


    Comment

    Working...