Thursday, June 23, 2011

FTP Plugin for PhoneGap Android

I did this FTP plugin awhile back but forgot to push it to the main phonegap-plugin's repository. I've pushed it over now and I'm putting together this quick blog post to explain how to use it.

The ftp client allows you to upload and download files from within your PhoneGap Android application. It provides basic get and put functionality.

1. To install the plugin, move www/ftpclient.js to your project's www folder and include a reference to it in your html file after phonegap.{ver}.js.

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>

<script type="text/javascript" charset="utf-8" src="ftpclient.js"></script>

2. Create a directory within your project called "src/com/phonegap/plugins/ftpclient" and copy "src/com/phonegap/plugins/ftpclient/FtpClient.java" into it.

3. Add the following activity to your AndroidManifest.xml file. It should be added inside the <application> tag.

<activity android:name="com.phonegap.DroidGap" android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>

4. Copy "libs/commons-net-2.2.jar" into the libs directory within your project. You will also need to right click on this file in eclipse and add the jar to the build path.

Once installed properly, you should be able to upload files using the following syntax:

window.plugins.ftpclient.put("test.txt", "ftp://username:password@ftp.server.com/test.txt;type=i", win, fail);

and of course download files using:

window.plugins.ftpclient.get("test.txt", "ftp://username:password@ftp.server.com/test.txt;type=i", win, fail);

I'm not sure how many people really need FTP functionality but it was an interesting exercise none the less.

8 comments:

Patrick Mueller said...

I need a Gopher plugin.

Simon Mac Donald said...

Haha...remind me to tell you the story of when I *almost* invented Amazon.com back in University. I local Halifax bookseller was taking advantage of Gopher to sell books all across the world and I pitched to him taking his business to the web but he didn't see the benefit of it.

I coded this up back when we were doing the HTTP FileTransfer code for PhoneGap and someone had requested FTP as well.

Christian Larsen said...

This is perfect now that PhoneGap apps runs out of memory, when uploading large files using FileTransfer.
Using this method it is now possible to upload large files.
Is is possible to output upload progress somehow?

Simon Mac Donald said...

Even though I wrote it, I haven't played around with it too much. Mostly, I've used it for uploading logs from the device to my server. Your milage may vary on very large files.

I'm sure there would be some way to add a progress event to the code but I don't have the cycles to get to it right now.

Christian Larsen said...

Thank you for your answer, I have searched for a version for iOS. Do you know if there i such a version or do you have any plans of making one?

Simon Mac Donald said...

No version that I know of and I won't be doing one for iOS. You'd have to ask on the Google Group to see if anyone has built one or plans on building one.

jamie macdonald said...

Hi simon i am getting an "10 error" when i run the ftp code any ideas

Simon Mac Donald said...

@Jamie, no immediate ideas. Let's take this over to the google group.