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.

20 comments:

  1. 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.

    ReplyDelete
  2. 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?

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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?

    ReplyDelete
  5. 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.

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

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

    ReplyDelete
  8. Hi simon i am getting an "IO error" when i run the ftp code .Please help me ASAP

    ReplyDelete
  9. @Deepika

    I need more details. I haven't seen how you are calling the plugin or what the logs are.

    ReplyDelete
  10. @simon

    thanks for your plugin.

    I've tried to put it at work with no success. Well, I got the win callback on .get operation but then the file on /sdcard is empty. Same result on both cordova 1.9 and 2.0.

    any idea? thanks in advance

    ReplyDelete
  11. @Simone Zappatini

    What do you see in "adb logcat"?

    ReplyDelete
  12. @Simone Zappatini

    What do you see in "adb logcat"?

    ReplyDelete
  13. @simon

    solved. I had to change

    filename = filename.substring(0, filename.length() - 7);

    to:

    filename = filename.substring(1, filename.length() - 7);

    in FtpClient.java, method extractFileName().
    Thanks anyway
    simone

    ReplyDelete
  14. Hi Simon,
    can i use this plugin without use Eclipse but only Cordova js file and html in Dreamweaver?

    ReplyDelete
  15. @pas

    Sorry, I don't know a dang thing about Dreamweaver.

    ReplyDelete
  16. This looks like a promising plugin for file transfer.

    But How can I use this with build.phonegap.com ?

    ReplyDelete
  17. @hussain bharmal

    You can't unless the build team makes it available. Things in PhoneGap 3.0 will make adding plugins to your project much easier.

    ReplyDelete
  18. Hi Simon, thanks for the plugin.. it works perfectly to upload files to a ftp server, but I want to upload to a Sftp server.. which is not happening. How could I go about with this? any help would be greatly appreciated.. Thanks in advance.

    ReplyDelete
  19. @at_if7

    You'll probably need to go into the Java code for the plugin to call the right methods in the Apache FTP library the plugin uses.

    ReplyDelete