I've set up a page that allows users to either take a photo or choose one from their phone's gallery, and it's functioning as expected. However, I'm looking to now upload the selected photo to my server on my Godaddy hosting. To achieve this, I integrated the Cordova file transfer plugin by executing the following command in the terminal:
cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git
Afterwards, I wrote a brief snippet of code to upload the selected photo, but I'm not receiving any alert messages (neither error nor success).
Here is the code snippet used to select an image:
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Display the captured photo
// The in-line CSS rules are used to resize the image
//
largeImage.src = imageURI;
upload();
}
Code for the Upload function:
function upload() {
alert('large');
var uploadingImage = document.getElementById('largeImage');
var imgUrl = uploadingImage.src;
window.resolveLocalFileSystemURI(imgUrl, resolveOnSuccess, fsFail);
options = new FileUploadOptions();
// parameter name of file:
options.fileKey = "my_image";
// name of the file:
options.fileName = imgUrl.substr(imgUrl.lastIndexOf('/') + 1);
// mime type:
options.mimeType = "image/jpeg";
params = {val1: "some value", val2: "some other value"};
options.params = params;
ft = new FileTransfer();
ft.upload(fileuri, "http://siencelb.org/raycoding/insurance/avatar", success, fail, options);
}
function resolveOnSuccess(entry) {
fileuri = entry.toURL();
//use fileuri to upload image on server
}
function fsFail(message) {
alert("Error Message: " + message + "Error Code:" + message.target.error.code);
}
I have included two buttons on the page - the first button allows users to select an image, which is then displayed in the 'largeImage' div and this functionality is working correctly. The second button is intended to upload the selected image. It's worth noting that the alert('large') message is being displayed.