webOS file upload

This is something pretty basic, but the example in the API documentation leaves a lot to be desired and, if I recall correctly, doesn’t even work.

So here’s how to do a file upload on webOS, sending the file along with additional data in the POST call.

One critically important point to take note of is that session information is not passed from your app to the com.palm.downloadmanager service so, if necessary, you must verify and authenticate the user again. As I was using PHP for my server-side stuff, I looked into getting and passing the PHPSESSID cookie, but I couldn’t figure out how the get the value, webOS does not seem to store it with other cookies.

// additional POST data
// spot_id, userName, password declared elsewhere
var post_params = [
    {
'key': 'id', 'data': spot_id, 'contentType': 'text/plain' },
    {
'key': 'username', 'data': userName, 'contentType': 'text/plain' },
    {
'key': 'password', 'data': password, 'contentType': 'text/plain' }
];    



ctrlr.serviceRequest(
'palm://com.palm.downloadmanager/', {
method:
'upload',

// myfile = fileName of file to upload
// url_to_web_service = URL to server-side script that will handle file upload
// Note, contentType is mime type of file (according to docs), but 'img' used for images?!
parameters: {
'fileName': myfile,
'fileLabel': 'my_cool_file',
'url': url_to_web_service,
'contentType': 'img',
'subscribe': true,
'postParameters': post_params
},

onSuccess:
function (resp)
{
if (resp.responseString) {
// file has been uploaded             
}
else {
// file partially uploaded, resp contains progress info
}
},

onFailure:
function (e)
{
// something bad happened!
}
});


On the server-side, the file is identified by the value of ‘fileLabel’ (in this case ‘my_cool_file’), and the additional POST data is identified by the value of the key field.

18 Comments

  1. […] This post was mentioned on Twitter by John Stephens. John Stephens said: RT @AvishkarAA: webOS file uploads, http://bit.ly/ePtLt4 #webOS #webOSdev […]

  2. Ganesh

    What if i want to upload a image from wallpapers to server what will be the correct code

  3. Avishkar Autar

    @Ganesh, same code, you just have to specify the wallpaper filename, which should be something like:

    /media/internal/wallpapers/FILENAME.jpg

  4. Ganesh

    thanks for the reply
    i am using this code

    this.controller.serviceRequest(‘palm://com.palm.downloadmanager/’, {
    method: ‘upload’,
    parameters: {
    ‘fileName’: “/media/internal/wallpapers/12.jpg”,
    ‘fileLabel’: ‘gka12’,
    ‘url’: ‘http://www.somewebsite/wrk’,

    ‘contentType’: ‘img’,
    subscribe: true
    },
    onSuccess: function (resp){
    Mojo.Log.info(‘Success : ‘ + Object.toJSON(resp));

    },
    onFailure: function (e){
    Mojo.Log.info(‘Failure : ‘ + Object.toJSON(resp));

    }.bind(this)
    });

    Now i am trying to access the file at server using gka12 which is the ‘fileLabel’ but with no success ,please help

  5. Avishkar Autar

    That looks fine.

    Are you getting an error message in the logs?

  6. Ganesh Kalikar

    Thanks for replying .
    The error that i am getting is “HTTP Error 405 – The HTTP verb used to access this page is not allowed”
    Is any sort of server side scripting is required ?
    Please help ?

    Thanks again

  7. Avishkar Autar

    Yes, you must have a server-side script to handle the file upload.

    In PHP, for example, you’d get the information about the file in $_FILES[‘gka12’]
    e.g. path to uploaded file: $_FILES[‘gka12’][‘tmp_name’]

  8. Ganesh

    Thank you very much for all your replies.
    As per your tip i have used the following code in server side.

    //Retrieve Data via POST

    Dim sData as String = “”

    //Get Image Data
    sData = Request.Form(“gka12”)

    If (sData “”) Then
    Response.Write(“Read data via POST: ” + sData.ToString)
    End If

    But i am not able to get image data .
    Can you please help me with ASPX code in retrieving image data
    Thanks again for all your support and help.

  9. Avishkar Autar

    I’m not too familiar with ASP.NET, but for uploaded files I think the file info will be in Request.Files

    You won’t get the image data directly, you’ll only get the filename, file size, etc.
    To get the pixels, the easiest way would be to use the Bitmap class
    (see: http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx)

  10. Ganesh Kalikar

    Thanks for all your support.

  11. Ganesh

    It helped and i uploaded my image successfully as i got hint from the link you sent.
    Thanks a lot for taking your time out and helping me .
    Thanks .

  12. Avishkar Autar

    No problem.
    Glad I could help.

  13. Faran

    I am doing the same thing however the image is being uploaded to server as I can see it come but I am not getting any response from in resp.responseString. that is my main problem as my server is supposed to send back an xml response which i need to parse for my app but the responsestring seems to be empty!

  14. Avishkar Autar

    Just a guess, but make sure your sending the correct header from your server-side script.

    Content-type: application/xml; charset=utf-8

  15. Ganesh

    Hi this is ganesh once again.

    I wanted to integrate facebook in my webOS app.i want to just send message from my app to my facebook.
    Is it possible in !.4.5 sdk or 2.0 or higher version is required .
    How to integrate it in my app.
    I will be glad and thankful to you for any help.

    Thanks.

  16. Avishkar Autar

    You have to look into the Facebook API.
    see http://developers.facebook.com

    There is no built in facebook functionality for apps in the SDK. You’ll have to make the request calls to the facebook API yourself, just like any AJAX request; this can be done with any version of the webOS SDK.

  17. Ganesh

    Thanks very much for your reply.I have not solved it completely but working on it.
    I wanted another help also ,i have problem getting the access token for twitter in my webOS app,
    Any suggestion would be very helpful.
    Thanks.

  18. Avishkar Autar

    The access token is just a string,

    var accessToken = “….”;

    What issue are you having?