2

I`m trying to find out how to send images to my back-end server using Worklight adapters. I know that I can send them through Worklight adapters using Base64 encoding but this implies in around 30% more traffic between the servers and some undesired processing overhead.

For now I`m using the Phonegap FileTransfer library as I show below, but this creates a directly connection between the client and the back-end server not going through Worklight server as I want.

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";

var headers = {"Content-Type": "image/jpeg"};
options.headers = headers;

var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(host + "/images"), imageUploadSuccess, imageUploadFail, options);

function imageUploadSuccess(r) {
    WL.Logger.debug("Submit success! HTTP Status Code = " + r.responseCode);
    WL.Logger.debug("Response = " + r.response);
    WL.Logger.debug("Bytes sent = " + r.bytesSent);
    $.mobile.changePage('#SuccessPage');
}
function imageUploadFail(error) {
    WL.Logger.debug("submit error! source = " + error.source);
    WL.Logger.debug("target = " + error.target);
    $.mobile.changePage('#FailPage');
}

Is there a way that I can do that?

Thank you in advance.

-- Edit --

Another problem that occurs is that when my backend server receives the file, it seems corrupted and cannot be readed as an image.

felipeh
  • 88
  • 10
  • If you want to go through the Worklight Server, your only way is Worklight Adapters. – Idan Adar Nov 05 '13 at 13:42
  • If your concern is the increased size of the payload because of the encoding, you can request your response to be compressed by using the `compressResponse` option in your `invokeProcedure` – Srik Nov 05 '13 at 14:59
  • Thank you for the comments. @IdanAdar, that`s what I wanted to know, if I could use the adapters to send image without encoding. @Srik, but the compression would be applied only in the response (JSON) and not in the image that I want to send with an HTTP POST. – felipeh Nov 05 '13 at 16:28
  • Sorry, I misunderstood your question. Currently, I can't think of a way of sending binary data to the Worklight Server without base64 encoding that is because JavaScript doesn't offer a direct way to deal with binary streams. – Srik Nov 08 '13 at 11:00
  • @felipeh any update? any solution ? when reading http://gonzalo123.com/2013/10/28/taking-photos-with-a-phonegapcordova-application-and-uploading-them-to-the-server/ I can see also that base64 images can cause memory issues on device also. Can Cordova file transfer be part of the solution? – WiPhone Aug 24 '14 at 08:47

1 Answers1

1

At this time, Worklight adapters do not support sending data in binary form.

This means that currently your only option is the one you do not like, which is to base64 encode the image file and store the resulting string in the database and when you need to use it, to base64 decode it.

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • I am using ibm mobile first 8.0, can I send binary data to my Adapter using javascript client apis inside payload ? – Nishant Tanwar Dec 19 '16 at 12:47
  • Maybe this will help: https://mobilefirstplatform.ibmcloud.com/blog/2016/12/05/howto-handle-large-files-with-ibm-mobilefirst-foundation/ – Idan Adar Dec 19 '16 at 12:48