I have installed plugin cordova-plugin-contacts
to fetch a single contact.
Here is the code snippet
$('body').on("click", "#btnAttachment", function () {
navigator.contacts.pickContact(function(contact){
alert(JSON.stringify(contact));
},function(err){
console.log('Error: ' + err);
});
});
I am getting all things in contact
object.
Now I want to upload a contact on server as a vcf file or by any other suitable method. I have the url provided just want to make a post call. Can anyone help me on this ?
You can use ajax to upload contact to server.
$.ajax({
url: API_URL + 'url-to-server',
type: 'post',
data: {
contact_name: "value-for-parameter-contact_name",
contact_number: "0123456789",
contact_email: "[email protected]",
contact_other_detais: ""
}, error: function(){
},
success: function(data) {
//this function will call on success.
},
});
you can find more help about ajax here: http://www.w3schools.com/jquery/ajax_ajax.asp
Edited:Using file
If you really want to do that you can done by following way:
You need to install cordova-plugin-file-transfer https://github.com/apache/cordova-plugin-file-transfer This can be used for create vCard and upload to server.
if you want Device automatically handle vCard file, You need to follow RFC 6350 vCard instruction. https://en.wikipedia.org/wiki/VCard or https://www.rfc-editor.org/rfc/rfc6350
//p is your person object(have all contact details).
function writeFile(p) {
var type = window.TEMPORARY;
var size = 1*1024*1024; // file size
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
// The path for contact.vcf file is:\data\data\'your-package-name-goes-here'\cache
fs.root.getFile('contact.vcf', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
alert('Write completed.');
// Now you can upload this file to server
upload(fileEntry);
};
fileWriter.onerror = function(e) {
alert('Write failed: ' + e.toString());
};
var vCardFile ="BEGIN:VCARD\r\nVERSION:3.0\r\nN:" + p.getSurname() + ";" + p.getFirstName() + "\r\nFN:" + p.getFirstName() + " " + p.getSurname() + "\r\nORG:" + p.getCompanyName() + "\r\nTITLE:" + p.getTitle() + "\r\nTEL;TYPE=WORK,VOICE:" + p.getWorkPhone() + "\r\nTEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "\r\nADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "\r\nEMAIL;TYPE=PREF,INTERNET:" + p.getEmailAddress() + "\r\nEND:VCARD\r\n";
fileWriter.write(vCardFile); //vCardFile is your Formatted string VCF format.
}, errorCallback);
}, errorCallback);
}
function errorCallback(error) {
alert("ERROR: " + error.code)
}
}
function upload(fileEntry) {
var fileURL = fileEntry.toURL();
var success = function (r) {
console.log("Successful upload...");
console.log("Code = " + r.responseCode);
// displayFileData(fileEntry.fullPath + " (content uploaded to server)");
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
// SERVER must be a URL that can handle the request, like
// http://some.server.com/upload.php
ft.upload(fileURL, encodeURI(SERVER), success, fail, options);
};
After uploading, you can delete temporary vCard file using:
fileEntry.remove(function() {
alert('File removed.');
}, errorCallback);