web-servicesdrupaldrupal-7titaniumdrupal-services

Upload an image to Drupal 7 / Services 3 from Titanium?


This must be close but I can't figure out what's causing the error.

In my Titanium app, I have a Webview with a canvas element and this code:

function getImageData() {
    return canvas.toDataURL('image/png');
}

I am moving that data to the Titanium app like this:

var imageBase64data = webview.evalJS('getImageData()')

The data looks good starting with "data:image/png;base64,"...

Then in Titanium, I have a logged-in drupal session and call this function:

function uploadImage(imageBase64data, callback) {

    var url = REST_PATH + "file.json";

    var file = { 
        filename: utils.createRandomString() + ".png",
        file: imageBase64data
//      uid: Titanium.App.Properties.getString("userUid"),
//      filesize: ""+Titanium.Utils.base64decode(imageBase64data).length,
    };


    var xhr = Titanium.Network.createHTTPClient({timeout: 30000});

    xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    var authString = Titanium.App.Properties.getString("userSessionName")+'='+Titanium.App.Properties.getString("userSessionId");
    xhr.setRequestHeader("Cookie", authString);

    xhr.onload = function() {
        if(xhr.status == 200) {

            var response = xhr.responseText;

            callback(response);
        }
    };

    xhr.onerror = function(e) {
      alert("There was an error: " + e.error);

      Ti.API.info(JSON.stringify(this));
    };    

    xhr.open("POST", url);
    xhr.send(file);
}

xhr.onerror is being called with e.error = "undefined"

The trace looks like this:

{
  "responseData":{},
  "readyState":4,
  "connected":true,"UNSENT":0,"DONE":4,"HEADERS_RECEIVED":2,"OPENED":1,"LOADING":3,
  "responseText":null,"status":406
}

I think authentication is working because I was previously getting a "need authentication" error until I added the Cookie header.


Solution

  • That was with the installation provided by Drupanium. I just did a fresh Drupal and fresh Services 3 install and my file is uploading nicely.