This cordova plugin is created to use ftp (client) in web/js.
Support both iOS and Android platform now.
You can do the following:
- List a directory
- Create a directory
- Delete a directory (must be empty)
- Delete a file
- Download a file (with percent info)
- Upload a file (with percent info)
- Cancel upload/download
$ cordova plugin add cordova-plugin-ftp
$ cordova prepare
Dependency:
- For iOS, the plugin depends on CFNetwork.framework, which has been added to plugin.xml (and
cordova prepare
will add it to platfrom project), so you don't need to do anything. - But for Android, it depends on com.android.support:support-v4:23.2.0, which should be added to your platfrom project (e.g. in Android Studio) by hand.
- @angular/core (included by default on ionic2)
- lodash (included by default on ionic2)
- rxjs/Observable (included by default on ionic2)
- rxjs/Subscriber (included by default on ionic2)
You can access this plugin by typescript class Ftp
.
ionic cordova plugin add https://github.com/olaferlandsen/cordova-plugin-ftp.git
ionic plugin add https://github.com/olaferlandsen/cordova-plugin-ftp.git
cordova plugin add https://github.com/olaferlandsen/cordova-plugin-ftp.git
import '../../plugins/cordova-plugin-ftp/types/ftp';
import '../../plugins/cordova-plugin-ftp/types/ftp';
@Component({
selector : 'page-home',
templateUrl : 'home.html'
})
export class HomePage
{
/**
*
*/
constructor (public navCtrl: NavController, public ftp: Ftp)
{
}
/**
*
*/
ionViewDidLoad() {
this.ftp.isAvailible().then(() => {
this.ftp.connect('127.0.0.1', 'root', 'password').then(() => {
this.ftp.upload('file.zip', '/var/www/file.zip').then(() => {
this.ftp.download('file.zip', '/var/www/file.zip').then(() => {
this.ftp.rm('/var/www/file.zip').then(() => {
this.ftp.disconnect();
})
})
})
});
}, () => {
// ups... FTP plugin is not available...
})
}
}
- static isAvailible ():Promise
- static basename (path:
string
, suffix?:string
):string
- static dirname (path:
string
):string
- static connect (hostname:
string
, username:string
, password:string
):Promise - static upload (local:
string
, remote:string
):Promise - static uploadWithProgress (local:
string
, remote:string
):Observable - static download (local:
string
, remote:string
):Promise - static downloadWithProgress (local:
string
, remote:string
):Observable - static rm (remote:
string
):Promise - static rmdir (remote:
string
):Promise - static ls (remote:
string
):Promise - static mkdir (remote:
string
):Promise`` - static cancel ():Promise
- static disconnect ():Promise
You can access this plugin by js object window.cordova.plugin.ftp
.
window.cordova.plugin.ftp.connect('hostname.com', 'username', 'password', function () {
window.cordova.plugin.ftp.upload('file.zip', '/var/www/file.zip', function (progress) {
if (progress === 1) {
window.cordova.plugin.ftp.download('file.zip', '/var/www/file.zip', function() {
window.cordova.plugin.ftp.rm('/var/www/file.zip', function() {
// :D
})
})
}
})
})
- For iOS,
ftp.connect
will always succeed (even ifusername
andpassword
are incorrect), but it does NOT mean the later actions, e.g.ls
...download
will succeed too! So check theirerrorCallback
carefully. - Want to upload/download multiple files? The plugin just inits one connection and transmit all files via that connection. If you use asychronous syntax (e.g.
foreach
) to start multiple upload/download in a short time, it will mess the transfer. Instead, you can try $q or async to transmit one after one.
Implement ftp.disconnect
or ftp.reconnect
later, to fix "too much connect from this client...".
- The iOS native implementing is based on GoldRaccoon.
- The Android native implementing is based on ftp4j jar (LGPL).