Skip to content

Instantly share code, notes, and snippets.

@heyMP
Created April 9, 2014 01:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyMP/10218319 to your computer and use it in GitHub Desktop.
Save heyMP/10218319 to your computer and use it in GitHub Desktop.
Upload photo to Drupal Services
// PHOTOS Upload
var imagesrc;
function takePhoto() {
navigator.camera.getPicture(onSuccess, onFail, { quality: 30, destinationType: Camera.DestinationType.DATA_URL, correctOrientation:true });
}
function onSuccess(imageData) {
var image = document.getElementById('photo-deposit');
imagesrc = imageData;
uploadthePhoto();
}
function onFail(message) {
alert('Failed because: ' + message);
$.mobile.hidePageLoadingMsg();// show loading message
}
function uploadthePhoto() {
newImageSrc = imagesrc;
$.mobile.showPageLoadingMsg();// show loading message
$.ajax({// upload the picture
url: 'http://keystone.niswebsolutions.com/api/file.json',
type: 'POST',
data: '{"uid":"1","filesize":"99999","filename":"work-order-photo.jpg","file":"'+newImageSrc+'"}',
dataType: 'json',
contentType: 'application/json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.mobile.hidePageLoadingMsg();// show loading message
alert('Failed');
console.log(JSON.stringify(XMLHttpRequest));
console.log(JSON.stringify(textStatus));
console.log(JSON.stringify(errorThrown));
},
success: function (data) {
console.log(JSON.stringify(data));
var fid = data.fid;
$.ajax({// get node info
url: 'http://keystone.niswebsolutions.com/api/node.json',
type: 'POST',
data: '{"title":"Work Order Picture","type":"work_order_picture","field_image_picture":{"und":[{"fid":'+fid+'}]},"field_work_order_reference":{"und":[{"nid":"[nid:'+nid+']"}]},"uid":"1","language":"und"}',
dataType: 'json',
contentType: 'application/json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
$.mobile.hidePageLoadingMsg();// show loading message
alert('Failed');
console.log(JSON.stringify(XMLHttpRequest));
console.log(JSON.stringify(textStatus));
console.log(JSON.stringify(errorThrown));
},
success: function (data) {
$.mobile.changePage($("#page_node_pictures"), {allowSamePageTransition:true});
$.mobile.hidePageLoadingMsg();// show loading message
}
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment