Solved

Creating a file in the FTP Folder using SFTP

Posted on 2014-09-12
6
500 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
  • 4
  • 2
6 Comments
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
@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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:chokka
Comment Utility
Thanks for sharing your answer.
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
did you succeed with relative path?

Sara
0
 

Author Comment

by:chokka
Comment Utility
@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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Cloud file services can fill many different roles for your business. Often, the use of cloud file services begins with employees using consumer products, like Dropbox, to share files with customers and each other. While sync-and-share can be an effe…
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.
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…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now