Learn how to use the multiple methods and utilities that this plugin offers.

With the client object generated with the createSFTPClient method, you can execute the tipical tasks that SFTP allows to do.

client.setCredentials

As always, you need to provide the credentials to access your remote SFTP server. The setCredentials method expects up to 4 arguments:

  1. The IP or Domain of your SFTP server e.g india123.systems.in
  2. The username used to login e.g root
  3. The password used to login.
  4. The Port of the connection (default to 22).
// Set the credentials like this : ip-sftp-server , your user, the password
client.setCredentials("00.000.00.00","user","password");
// or client.setCredentials("myserver.domain.com","user","password");

// If your sftp server doesn't use the port 22 you can change it with setCredentials method
// in this case we will change the port to the 21
client.setCredentials("00.000.00.00","user","password","21");

The other methods will use the set credentials in the instance of the client.

client.setPath

If you want to list a remote folder, you need to provide as first a path. This can be set as shown in the following example:

client.setPath("/var/www/vhosts/myproject");

client.getPath

You can retrieve the current path used to list a directory with the getPath method. If the path wasn't previously set, then it will be always "/root":

// /root
console.log(client.getPath());

client.setPath("/var/www/vhosts/myproject");

// /var/www/vhosts/myproject
console.log(client.getPath());

client.list

The list method allows you to list a remote directory (folders and files). The path is retrieven from the getPath method of the client:

client.setPath("/var/www/vhosts/myproject");

/**
 * Receives an array with objects with all the content of a path (files and folders)
 */
var success = function(data) {
    console.info(data);
    /**
    Outputs : 
    [
        {
            name:"Folder/File name",
            filepath: "/var/www/vhosts/myproject/something.txt",
            isDir:false, // is Folder = true, is File = false
            isLink:false,
            size:"123", // bytes
            permissions: "????",
            permissions_string:"xxxxx",
        }
    ];
    */
};

// Oops .. something failed :| , may be the path ? or credentials ?
var failure = function(e) {
    console.error(e);
};

client.list(success, failure);

client.downloadFile

The downloadFile method allows you to download a file from the server from its remote directory. The method expects up to 3 arguments:

  1. The remote path of the file e.g /var/www/vhosts/project/file.txt
  2. The local path for the file (it can have a new name, but note that the file:// can't be included) e.g /storage/emulated/0/mynewname.txt
  3. An object with the callbacks:
    • The success callback
    • The error callback
// Do not include file:// to the local path, you can change the local filename if you want
client.downloadFile("/var/www/vhosts/myproject/file.txt","/storage/emulated/0/file.txt",{
    success:function(download){
        // see the object info
        console.log(download);

        if(download.finished == true){
            console.info("The file has been succesfully downloaded");
        }else{
            //Display the progress
            console.log("Progress download : "+download.progress+"%. "+ download.bytesprogress +" bytes downloaded of " + download.filesizebytes + "total");
        }
    },
    error:function(er){
        // snap ! An error :(
        console.error(er);
    }
});

As you can see the success calback receives an object, this object show basic information about the progress and if the download has been finalized.

client.uploadFile

The uploadFile allows you to upload a local file (of the device) to a remote path in your SFTP server easily. The method expects up to 3 arguments:

  1. The local path for the file  e.g /storage/emulated/0/file.txt
  2. The remote path of the file (it can have a new name, but note that the file:// can't be included) e.g /var/www/vhosts/project/mynewname.txt
  3. An object with the callbacks:
    • The success callback
    • The error callback
// Do not include file:// to the local path, you can change the remote filename if you want
client.uploadFile("/storage/emulated/0/file.txt","/var/www/vhosts/myproject/file.txt",{
    success:function(upload){
        // see the object info
        console.log(upload);

        if(upload.finished == true){
            console.info("The file has been succesfully uploaded");
        }else{
            //Display the progress
            console.log("Progress upload : "+upload.progress+"%. "+ upload.bytesprogress +" bytes uploaded of " + upload.filesizebytes + "total");
        }
    },
    error:function(er){
        // snap ! An error :(
        console.error(er);
    }
});

client.removeFile

The removeFile method allows you to delete a file from your SFTP server. It expects as first argument the path to the remote file and as second parameter an object with the callbacks.

client.removeFile("/var/www/vhosts/myproject/file.txt",{
    success:function(removed){
        // see the object info
        console.log(removed);

        if(download.deleted == true){
            console.log("File removed from the server");
        }
    },
    error:function(er){
        // snap ! An error :( maybe doesnt exist?
        console.error(er);
    }
});

client.setIdentity

If your server needs a Private key to connect, you can use setIdentity method, Public key authentication uses a public-private , your private key must use OpenSSH format.

client.setIdentity("/storage/emulated/0/id_dsa.pub");

client.setKnownHosts

It's recommendable to set a Known Hosts file to your sftp connection, otherwise your connection can be victim of a Man In the Middle Attack (MITM), just use setKnownHosts method giving the path of the local file. If you don't use this file, the plugin will work anyway, but is better use a known hosts.

// Set local known_hosts file
client.setKnownHosts("/storage/emulated/0/known_hosts");

client.createFolder

You can create dinamically a folder in your remote server with this method. It expects as first argument the path to the remote folder (note that the parent folder needs to exist) and as second parameter an object with the callbacks:

// Create the test-folder
// The folder "/var/www/vhosts/main/" needs to exist
client.createFolder( "/var/www/vhosts/main/test-folder" ,{
    success:function(status){
        // see the object info
        console.log(status);

        if(status.created){
            console.log("Folder created");
        }
    },
    error:function(er){
        // snap ! An error :(
        console.error(er);
    }
});

client.removeFolder

You can delete an existent folder in your server by using the removeFolder method. It expects as first argument the path to the remote folder that will be removed and as second parameter an object with the callbacks:

// Delete folder
client.removeFolder( "/var/www/vhosts/main/test-folder" ,{
    success:function(status){
        // see the object info
        console.log(status);

        if(status.deleted){
            console.log("Folder removed");
        }
    },
    error:function(er){
        // snap ! An error :( maybe doesnt exist?
        console.error(er);
    }
});