Blog

    Happenings and acts of geekery.

PhoneGap, FileTransfer, and ColdFusion

I like PhoneGap. Really. But I bashed my head on this for a while so if this saves you the headache, you're welcome!

The app I am working on uses the Camera API to capture an image and then submits it to a back-end service for inclusion in an online photo gallery. Pretty simple.

When you capture the image, you have two choices for how to handle the data:

Camera.DestinationType.DATA_URL
Camera.DestinationType.FILE_URI

The problem with DATA_URL is that the cameras on mobile devices are getting to be too darn good. That DATA_URL is a Base64 encoded string that can very quickly run you straight out of memory. Therefore, you are strongly encouraged to use FILE_URI instead which is simply a pointer to a file on the device file system.

How do you then get this file moved up to the server? Use the File API's FileTransfer object.

var imageURI = PATH_TO_FILE_AS_GIVEN_BY_CAMERA;

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
/* ColdFusion barfs if you don't set this */
options.chunkedMode = false;

var params = new Object();
params.formfield1 = 'send other data along for the ride';
   params.formfield2 = 'using more fields like this';

options.params = params;

var ft = new FileTransfer();
ft.upload(imageURI, "http://YOUR_URL_HERE/SOME_FILE.cfm", win, fail, options);
/* don't forget to create functions for win and fail conditions */

The key line here is options.chunkedMode = false;. From what I could tell by reading Ben's blog post , ColdFusion simply can't handle chunked form data. Two hours of searching and evaluating alternatives...and then I noticed this option.

So...lesson learned. Base64 will not only drag your app down but probably crash it as well. FileTransfer (sans chunks) is the way to go.

Comments
Huge help here. My app was crashing every time until I added this. Thanks!
# Posted By Eric | 11/11/11 12:00 AM
Hi,

no problem with cross domain ?
it would be great help if you make a simple full demonstration including cold fusion code.

regards
# Posted By Bader | 12/20/11 5:29 AM
it would be great help to me, but I need to send the Multiple images to server at a time to fill database. Please can you suggest me how to solve this.
# Posted By TejaVarma | 2/1/12 7:16 AM
it would be great help if you make a simple full demonstration including cold fusion code.
# Posted By hermes kelly | 3/16/12 1:13 AM
I second hermes request for a coldfusion, jquery mobile, phonegap demo.
# Posted By redlamp | 3/22/12 8:26 PM

About the blog