在phonegap中上传服务器上的联系人

时间:2016-12-13 07:15:41

标签: cordova file-upload cordova-plugins contacts

我已安装插件cordova-plugin-contacts以获取单个联系人。

以下是代码段

$('body').on("click", "#btnAttachment", function () {


     navigator.contacts.pickContact(function(contact){

         alert(JSON.stringify(contact));

     },function(err){
         console.log('Error: ' + err);
     });

});

我收到了contact对象中的所有内容。

现在我想将服务器上的联系人作为vcf文件或任何其他合适的方法上传。 我提供的网址只是想打个电话。 谁可以帮我这个事 ?

1 个答案:

答案 0 :(得分:3)

您可以使用ajax将联系人上传到服务器。

$.ajax({
    url: API_URL + 'url-to-server',
    type: 'post',
    data: {
        contact_name: "value-for-parameter-contact_name",
        contact_number: "0123456789",
        contact_email: "aa@xyz.com",
        contact_other_detais: ""
    }, error: function(){

          },
    success: function(data) {
        //this function will call on success.
    },

});

您可以在此处找到有关ajax的更多帮助:http://www.w3schools.com/jquery/ajax_ajax.asp

  

已编辑:使用文件

如果您真的想这样做,可以通过以下方式完成:

您需要安装 cordova-plugin-file-transfer https://github.com/apache/cordova-plugin-file-transfer 这可用于创建vCard并上传到服务器。

如果您希望Device自动处理vCard文件,您需要遵循RFC 6350 vCard指令。 https://en.wikipedia.org/wiki/VCardhttps://tools.ietf.org/html/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);
};

上传后,您可以使用以下方法删除临时vCard文件:

fileEntry.remove(function() {
            alert('File removed.');
         }, errorCallback);