webOS file upload
Jan 9 2011 ยท webOS
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.