Posted on by & filed under Content - Highlights and Reviews, Mobile Development.

If you followed the PhoneGap: Storing and Retrieving with the FileSystem tutorial, and got your friends to use our cool “friend describer” app, you may be wondering what the next step is. Now that we have a list of what people think of their friends, how do we turn that into a fully-fledged social network?

Of course, the answer is that we upload all of their data to our servers! Handily, PhoneGap provides an easy mechanism for file transfers, with the aptly named FileTransfer.

Setting up a server to take multipart requests is a bit beyond the scope of this tip, but let’s assume we have an endpoint we can hit at (ahem). Here’s a function that (assuming the PhoneGap deviceready event has fired) will upload the contents of database.db:

Fairly simple, no? There are a few things that can go wrong, so we’ll fix up the error callback to handle some more edge cases:

The error object that we receive back lets us know if there was an error doing the transfer — a bad file URL, target URL or connection. We will also want to check the response back from the server, which might have had it’s own errors:

The response object that gets passed back will also have the number of bytes transferred (response.bytesSent), in case you need that knowledge. In most cases, you’ll verify that you got the right response code from the server and move on.

It’s as easy as that to get your user’s data on your servers. Now go forth and get funding!

Safari Books Online has the content you need

Check out these Phonegap books available from Safari Books Online:

PhoneGap Beginner’s Guide, written by Andrew Lunny, the author of this post, will show you how to use the PhoneGap mobile development framework to target multiple mobile platforms: iOS, Android, BlackBerry, and more with a single application. With PhoneGap, you can use existing web development skills, instead of learning a new environment for every platform on the market.
Using HTML, CSS, and Javascript, PhoneGap allows you to jump into the mobile world and develop apps for iPhone, Android, and the BlackBerry, and Beginning PhoneGap will help show you how to best take advantage of PhoneGap.
Beginning PhoneGap: Mobile Web Framework for JavaScript and HTML5 is a definitive, one-of-a-kind book that teaches the fundamentals and strategies behind cross-platform mobile application development. Instead of learning languages like Objective-C, focus on building apps from day one for Android, iOS, Blackberry, WebOS and Symbian—without the complexities of these platforms.

About the Author

  Andrew Lunny is a software developer based in San Francisco, and the author of PhoneGap: Beginner’s Guide, from Packt Publishing. He currently works at Adobe on the PhoneGap Build project, building thousands of apps a day for five different mobile platforms. He is also responsible for writing cogent error messages when something inevitably goes wrong. You can follow him on Twitter at @alunny.

Tags: File Transfers, FileSystem, PhoneGap,

Comments are closed.