I want to upload an image from Phonegap app to Drupal 7 backend using Drupal services module. for testing purpose i have done it with core php and every thing work correctly but i need to do the same using Drupal services module.
I am doing a signup process where user can upload an image as profile picture from the mobile device and i need to access the signup data and image at drupal 7 backend.
Core PHP:-
<?php
// Directory where uploaded images are saved
$dirname = "uploads/";
// If uploading file
if ($_FILES) {
print_r($_FILES);
mkdir ($dirname, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
}
?>
Phonegap Code
function uploadFile() {
// Get URI of picture to upload
navigator.camera.getPicture( function(uri) {
try {
var img = document.getElementById('pimage');
img.style.visibility = "visible";
img.style.display = "block";
var imageURI = uri;
if (!imageURI || (img.style.display == "none")) {
document.getElementById('picture_msg').innerHTML = "Tap on picture to select image from gallery.";
return;
}
// Verify server has been entered
server = document.getElementById('serverUrl').value;
if (server) {
// Specify transfer options
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
// Transfer picture to server
var ft = new FileTransfer();
ft.upload(imageURI, server, function(r) {
document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
img.src = uri;
img.width = 100;
img.height = 100;
},
function(error) {
document.getElementById('picture_msg').innerHTML = "Upload failed: Code = "+error.code;
}, options);
}
}
catch(exce) {
alert(exce);
}
},
function(e) {
console.log("Error getting picture: " + e);
document.getElementById('bio_description').innerHTML = "Error getting picture.";
},
{
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);}