Solved

Creating a file in the FTP Folder using SFTP

Posted on 2014-09-12
6
658 Views
Last Modified: 2014-09-15
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

0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 40322871
according to the following link

     http://www.example-code.com/vbdotnet/sftp_realPath.asp

all files accessable by Chilkat.SFtp are below user's home folder.

if I understand the remarks correctly you either need to give a relative path like ./folder/folder/subfolder/xxx.csv or an absolute path which is not an ftp url but the absolute path after user login.

Sara
0
 

Author Comment

by:chokka
ID: 40323135
@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 ?
0
 

Author Comment

by:chokka
ID: 40323591
@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

0
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

Author Closing Comment

by:chokka
ID: 40323821
Thanks for sharing your answer.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40324170
did you succeed with relative path?

Sara
0
 

Author Comment

by:chokka
ID: 40324247
@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
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question