The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files from one host to another host over a TCP-based network, such as the Internet. FTP is built on a client-server architecture and uses separate control and data connections between the client and the server.
This plugin allows DroidScript to communicate with the FTP protocol and complete the most common tasks needed in a client-server relationship.
In order to use FtpClient, you must first load the plugin at the top of your script
using the LoadPlugin method like this:
app.LoadPlugin( "FtpClient" );
Then you can create an instance of the plugin object when you need it like this:
ftp = app.CreateFtpClient();
The first step to using FtpClient is to Connect to a FTP server.
The following methods are provided by the FtpClient object:
Version Information:
ftp.GetVersion();
Returns the plugin author and version.
Error Handling:
ftp.GetError();
Example - Display Error
if (ftp.RenameFile('image.jpg', 'myimage.png')) { app.Alert( 'Rename was successful.' )};
if (ftp.GetError()) { alert(ftp.GetError()); }
Returns either false (No error) or the details of the error. You should check this after making calls to the server to make sure no errors occurred.
Example - Connect/Disconnect/Directory
app.LoadPlugin("FtpClient");
var res = '';
function OnStart()
{
lay1 = app.CreateLayout("Linear");
Utxt = app.CreateText("Username:", .3, -1, "Bold");
lay1.AddChild(Utxt);
Uedt = app.CreateTextEdit("", 0.8, -1, "NoSpell,MonoSpace,SingleLine");
lay1.AddChild(Uedt);
Ptxt = app.CreateText("Password:", .3, -1, "Bold");
lay1.AddChild(Ptxt);
Pedt = app.CreateTextEdit("", 0.8, -1, "NoSpell,MonoSpace,SingleLine");
lay1.AddChild(Pedt);
Stxt = app.CreateText("Server:", .3, -1, "Bold");
lay1.AddChild(Stxt);
Sedt = app.CreateTextEdit("", 0.8, -1, "NoSpell,MonoSpace,SingleLine");
lay1.AddChild(Sedt);
lay2 = app.CreateLayout("Linear", "Horizontal,FillXY");
btn = app.CreateButton("Connect");
btn.SetOnTouch(CallPlugin1);
lay2.AddChild(btn);
btn2 = app.CreateButton("Disconnect");
btn2.SetOnTouch(CallPlugin2);
lay2.AddChild(btn2);
btn3 = app.CreateButton("Directory");
btn3.SetOnTouch(CallPlugin3);
lay2.AddChild(btn3);
btn4 = app.CreateButton("File Exist");
btn4.SetOnTouch(CallPlugin4);
lay2.AddChild(btn4);
lay2.SetMargins(0, 0.1, 0, 0);
lay1.AddChild(lay2);
Rscroll = app.CreateScroller(1.0, 0.25, 'FillXY');
Rscroll.SetBackColor("#FFFFFF");
Rtxt = app.CreateText('Server Response', 1, -1, "MultiLine,Left");
Rtxt.SetTextColor('#000000');
Rtxt.SetBackColor("#FFFFFF");
Rscroll.AddChild(Rtxt);
Rscroll.SetMargins(0, 0.1, 0, 0);
lay1.AddChild(Rscroll);
app.AddLayout(lay1);
ftp = app.CreateFtpClient();
ftp.SetOnResponse(HandleResponse);
ftp.SetPort('21');
ftp.SetServerTimeout('10');
app.ShowPopup( ftp.GetVersion() );
}
function CallPlugin1()
{
var u = Uedt.GetText();
var p = Pedt.GetText();
var s = Sedt.GetText();
var c = ftp.Connect(u, p, s);
if (c) { res = ''; app.ShowPopup('You were connected successfully'); }
if (ftp.GetError()) { app.ShowPopup(ftp.GetError()); }
}
function CallPlugin2()
{
app.ShowPopup(ftp.Disconnect());
}
function CallPlugin3()
{
var list = ftp.GetDirectoryList(null);
if (ftp.GetError()) { app.ShowPopup(ftp.GetError()); }
if (list) {
var len = list.length;
var s = '';
for(var i = 0; i < len; i++ )
{
var file = list[i];
s += "Name: " + file.name + "\nType: " + file.type + "\nSize: " + file.size + " bytes\n" + file.timestamp + "\n\n";
}
app.Alert(s);
}
}
function CallPlugin4()
{
var file1 = ftp.FileExists('index.html');
if (ftp.GetError()) { alert(ftp.GetError()); }
var file2 = ftp.FileExists('myimage.png');
if (ftp.GetError()) { alert(ftp.GetError()); }
var file1txt = (file1) ? 'Exists' : 'Not Found';
var file2txt = (file2) ? 'Exists' : 'Not Found';
app.ShowPopup('index.html ' + file1txt + "\nmyimage.png " + file2txt);
}
function HandleResponse(txt)
{
res += txt + "\n";
Rtxt.SetText(res);
}
This plugin was written by Chris Ferrell, it is free to use in your projects. A credit line if used would be nice :)
This plugin utilizes apache.commons.net, which is licensed under Apache License 2.0.