Search code examples
appceleratortrigger.io

Error uploading photo to Appcelerator ACS using Trigger.io


I'm trying to upload a photo to Appcelerator Cloud Services (ACS) storage using Trigger.io.

I can't figure out the correct syntax to use for the file object. I'm getting an error "Error: Syntax error, unrecognized expression: #[object Object]"

Here's my relevant code:

$("#photograph-record").on("click", function(){
    forge.file.getImage({source:"camera", width: 280, height: 280},function(file) {

    var data = {
          photo: file  //the ID of file input control
        };

    sdk.sendRequest('photos/create.json', 'POST', data, callback);
    });
});

Here's the Docs for the ACS Photo class - http://cloud.appcelerator.com/docs/api/v1/photos/create

Required Parameters - photo: the attached binary file

Since it needs to be a binary I tried "photo: forge.file.string(file)" (http://docs.trigger.io/en/v1.4/modules/file.html#modules-file), but got an error on the Appcelerator side "Photo parameter required for photo upload".

I have no problem passing the image into my App page views using forge.file.url, so I know there's no problems with the file object, it's just figuring out the correct syntax to pass it as a binary to the sdk.sendRequest call.

Any ideas on what I need to be passing in the data variable to get this to work?


Solution

  • The Appcelerator docs are pretty good here - it looks like they're expecting a POST parameter called photo which contains the binary image data.

    To do that using our request module:

    $("#photograph-record").on("click", function(){
        forge.file.getImage({source:"camera", width: 280, height: 280},function(file) {
            file.name = 'photo'; // the magic    
            forge.request.ajax({
                url: 'https://api.cloud.appcelerator.com/v1/photos/create.json',
                files: [file],
                success: function () { ... },
                error: function () { ... }
            });
        });
    });
    

    I don't see a way to use their JS library here, because they're expecting you to pass in the id of a HTML form element to get data from, but we're interacting with the camera or gallery directly...