Upload photos from iPhone using PhoneGap, jQuery, and Python

PhoneGap

Setup an HTML button to click.

<a onclick="uploadPhoto(pictureSource.PHOTOLIBRARY);">Photo Upload</a>

the click handler

// A button will call this function
//
function uploadPhoto(source) { // Retrieve image file location from specified source navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 25, destinationType: destinationType.DATA_URL, sourceType: source, allowEdit: true });
}

The callback handlers.

function onPhotoDataSuccess(imageData) {
  // upload the captured photo
  $.post(‘http://path/to/ajaxUpload’, {data:imageData}, 
                 function(data){alert(‘success’);});
}
 
 
// Called if something bad happens.
// 
function onFail(mesage) {
  alert(‘Failed because: ‘ + message);
}

the server side handler (cherrypy)

    @cherrypy.expose
    def ajaxUpload(self, **kwargs):
        """
        Accept miscellaneous iphone picture upload.
        """
        import os
        import base64
  filename = "test.jpg" tmpFileName = os.path.join( "/tmp/", filename ) tmpFile = open(tmpFileName , ‘wb’) data = base64.b64decode(kwargs.get(‘data’)) tmpFile.write(data) tmpFile.close()
  cherrypy.response.headers[‘Content-Type’] = ‘text/javascript’ return "some type of return message for your AJAX if you like"

Reference Material:

Image Upload using jQuery and Python

PhoneGap Camera API