Link to home
Start Free TrialLog in
Avatar of chokka
chokkaFlag for United States of America

asked on

Creating a file in the FTP Folder using SFTP

Using third party tool Chilkat, i am creating a csv file in the server. file transfer is taking place through sftp. I have mentioned in the uri link : ftp://home/folder/folder/subfolder/, to create the csv file.

Apparently, csv file is created in the root directory. ftp://home/user_id/

We expect the file to be created in the mentioned file folder. How should i fix this issue ?

Sample source code : http://www.example-code.com/vbnet/sftp_uploadFile.asp

Public Function UploadFileFromStream(ByVal data As Byte(), ByVal _fileName As String) As Boolean

        Try

            Dim sftp As New Chilkat.SFtp()
            Dim success As Boolean
            success = sftp.UnlockComponent("Anything for 30-day trial")

            sftp.ConnectTimeoutMs = 5000
            sftp.IdleTimeoutMs = 10000

            success = sftp.Connect(_uri, _port)

            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  Authenticate with the SSH server. Either Password or Public Key Authentication

            success = sftp.AuthenticatePw(_userId, _password)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp()
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

          
            Dim handle As String
            handle = sftp.OpenFile(_fileName, "writeOnly", "createTruncate")
            If (handle = vbNullString) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            success = sftp.WriteFileBytes(handle, data)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If
            'sftp.Disconnect()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

        Return True

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chokka

ASKER

@sara, thank you !!  I am very new to sftp.

Okay, I am willing to give either relative path or absolute path which you suggested.

Issue is in FTP, we can access the folder only by a link which starts with ftp://computername/folder/file.csv.   And to access this link, we have a username / password.



Whether the rule is same for sftp ?? How to set the relative or absolute path for sftp ?
Avatar of chokka

ASKER

@sara, another question. How to give a relative or absolute file path in vb.net for sftp.

''' <summary>
    '''  Passing variable data and filename as Input parameters.
    ''' </summary>
    ''' <param name="data"></param>
    ''' <param name="_fileName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function UploadFileFromStream(ByVal data As Byte(), ByVal _fileName As String) As Boolean

        Try

            Dim sftp As New Chilkat.SFtp()
            Dim success As Boolean
            success = sftp.UnlockComponent("Anything for 30-day trial")

            sftp.ConnectTimeoutMs = 5000
            sftp.IdleTimeoutMs = 10000

            ' uri is the filepath - For now : ftp://home/foldername/subfoldername/file.csv
            ' uri expected to take sftp folder path : /home/subfoldername/file.csv
            'Port number always 22

            success = sftp.Connect(_uri, _port)

            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  Authenticate with the SSH server. Either Password or Public Key Authentication

            success = sftp.AuthenticatePw(_userId, _password)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp()
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

          
            Dim handle As String
            handle = sftp.OpenFile(_fileName, "writeOnly", "createTruncate")
            If (handle = vbNullString) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            ' Variable : byte is written as .csv file 
            success = sftp.WriteFileBytes(handle, data)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If
            'sftp.Disconnect()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

        Return True

    End Function

Open in new window

Avatar of chokka

ASKER

Thanks for sharing your answer.
did you succeed with relative path?

Sara
Avatar of chokka

ASKER

@sara, Not really. You gave a tip about Absolute file path and relative file path.

Again , i am using a third party dll. Name of the dll is Chilkat sftp. I am not sure whether the chilkatsftp supports Absolute file path or relative file path.

One of their sample program:
http://www.cknotes.com/understanding-sftp-absolute-filepaths/

Right now, my file is copied in
/home/<user_id>

Open in new window


I have posted new question for file copy method in linux. I am planning to take it as a batch file which can be executed from .Net Program