Search code examples
ibm-mobilefirstmobilefirst-adapters

get an jpg image from a mobilefirst http adapter


im working in a mobilefirst adapter (6.3) that need to pass an image from an url that is in the internarl network file system(example :http://www.up.edu.pe/RED_Compartir/facebook.png) but i cant get the data correctly, the img data isnt in the text variable: Here is my server side code:

    function getImage(id){
            var input = {
                    method : 'get',
                    returnedContentType : 'plain',
                    path : '/someUrl/someUrl2/'+id+'.jpg'
                };
            return  {
                out: Base64.encode(WL.Server.invokeHttp(input).text)
            };
        }

Here is my client code to process de image:

function getImageFrom() {
    execMobileFirstAdapter("adapterName", "method", ["parameter"]).then(function (data){
        WL.Logger.debug("OK")
        var imageBase = data.invocationResult.out;
        document.getElementById('imageServer').setAttribute( 'src', 'data:image/jpeg;base64,'+ imageBase );
    }).fail(function(data){
        WL.Logger.debug("error");
    })

}

Is there a way to return the base64 from a jpg image from a mobilefirst adapter?

Ive used this example: https://www.ibm.com/developerworks/community/blogs/mobileblog/entry/ibm_worklight_adapter_accessing_an_image?lang=en

and Works perfectly but i need to do this only with JavaScript in the server. is this posible?


Solution

  • Is there a way to return the base64 from a jpg image from a mobilefirst adapter?

    I've used this example: https://www.ibm.com/developerworks/community/blogs/mobileblog/entry/ibm_worklight_adapter_accessing_an_image?lang=en

    and Works perfectly but i need to do this only with JavaScript in the server. is this possible?

    The easiest approach is in fact to implement it the way the example does it in the article you've provided.

    The only other way is to find a JavaScript lib that does base64 encoding and try to add this lib to the adapter and then use it.

    Note that JavaScript adapters do not support adding additional files to the adapter, so that means you need to take the entire implementation of whichever lib you'll find and put it inside your adapter code. Not so nice. Also does not guarantee it'll work.

    Additionally, are you calling your adapter from inside a Cordova plug-in? That was strange. Why? Why not just use the WL.Client.invokeProcedure API... just like in the article...