I want to upload a file along with some string values to server. I am unable to find the syntax how to send it.
For example I need to post data(“Name”, “Mobile Number”, “Address”, File) to server. How to send it to server. I tried using FileTransfer but it returns error.
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = attachedFile.substr(attachedFile.lastIndexOf('/') + 1);
//options.mimeType = "image/jpeg";
options.params = {
UserId: userId,
Name: "Rakesh",
Mobile Number: "5412314569",
Address: "India"
};
var headers = { 'Authorization': tokenID }; // token id is authorization key
options.headers = headers;
var ft = new FileTransfer();
ft.onprogress = function (progressEvent) {
if (progressEvent.lengthComputable) {
loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
} else {
loadingStatus.increment();
}
};
// Here attached File is the path of the file and ADD_NOTE_API is the API URL.
ft.upload(attachedFile, encodeURI(ADD_NOTE_API), win, fail, options);
function win(result) {
console.log("Response Image = " + result.response);
var toast = app.toast.create({
text: "Success in uploading data"
});
toast.open();
app.preloader.hide();
}
function fail(error) {
debugger
app.preloader.hide();
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
var toast = app.toast.create({
text: "Error in uploading data"
});
toast.open();
}
}