Topic
7 replies Latest Post - ‏2012-03-20T11:05:45Z by IdanAdar
SystemAdmin
SystemAdmin
570 Posts
ACCEPTED ANSWER

Pinned topic Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

‏2012-03-20T06:10:30Z |
Hi good day!

I was wondering how do i upload image files ( such as photos ) via HTTPAdaptor ?

The example WorklightStarter_jQueryMobile is an excellent example, but unfortunately
it only demos a GET request.

How do i POST an image file to a URL ? I would like to upload the image file in 2 situations:
1) After a user takes a photo, and uploads this photo.
2) A user browses his media library, selects a photo and uploads this photo.

I have managed to upload a photo via 1 and 2 using the PhoneGap API.

Now I would want to try to upload photos via Worklight's Adaptor API.

For a start, i understand that i need to specify the parameters object and invoke the WL.Server.invokeHttp method. For example:

var input = {
method : 'post',
returnedContentType : 'json',
path: 'myurlhere',

}

How do i specify the file to be uploaded? What other options do I have to specify?

Thanks!
  • IdanAdar
    IdanAdar
    288 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T07:06:35Z  in response to SystemAdmin
    Hi.

    After getting the image, you can convert it to base64 and then send it to your remote server.
  • SystemAdmin
    SystemAdmin
    570 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T07:33:06Z  in response to SystemAdmin
    Hi,
    by converting to base64 after getting the image, do u mean converting the file object
    to base64 ?

    If this is the case, will the image file being POSTed to a RESTful endpoint still receive an
    image file ?

    Best.
  • IdanAdar
    IdanAdar
    288 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T07:39:43Z  in response to SystemAdmin
    eugene
    Hi,
    by converting to base64 after getting the image, do u mean converting the file object
    to base64 ?
    Yes.
    If this is the case, will the image file being POSTed to a RESTful endpoint still receive an
    image file ?
    No. On the backend of the receiving end you will need to convert it back to an image.
  • SystemAdmin
    SystemAdmin
    570 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T07:51:18Z  in response to SystemAdmin
    Hi,
    thanks for your reply.

    Is there any way i can do a HTTP post ( similar to a multipart form )
    via the adaptor ?

    Best Regards.
  • IdanAdar
    IdanAdar
    288 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T08:31:17Z  in response to SystemAdmin
    GET and POST, yes.
  • SystemAdmin
    SystemAdmin
    570 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T09:57:55Z  in response to SystemAdmin
    Hi,

    I was not specific with my question, so apologies.

    How can i do a HTTP POST via the adaptor that works similar to a
    multi-part html form that uploads an image ?

    What are the parameters do i need to set ?

    Best Regards,
    Eugene
  • IdanAdar
    IdanAdar
    288 Posts
    ACCEPTED ANSWER

    Re: Upload an Image using Adaptor taken from Camera ( or from Media Folder ) via Worklight ?

    ‏2012-03-20T11:05:45Z  in response to SystemAdmin
    Unfortunately we do not support multi-part and file uploads.

    The way to do it is as mentioned above by converting your image to base64, so there is no "similar way". You do the conversion and "send the file" as a data object and handle it on the backend side.

    Have you looked at the Developer Guide, pages 122-123 for the invokeHTTP parameters?