通过手机上传服务器上的联系人 [英] upload contact on server in phonegap

查看:217
本文介绍了通过手机上传服务器上的联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了插件 cordova-plugin-contacts 来获取一个联系人。

这里是代码片段

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


navigator.contacts.pickContact(函数(contact){

alert(JSON.stringify(contact));

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

});

我在 contact 对象。

现在我想在服务器上以vcf文件或任何其他合适的方式上传联系人。
我提供的网址只是想打个电话。
任何人都可以帮助我吗?

解决方案

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

<$ p 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:
} ,错误:函数(){

},
成功:函数(数据){
//此函数将调用成功
},

});

你可以在这里找到更多关于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/VCard
$: b $ b

  // p是您的个人对象(具有所有联系信息)。 
函数writeFile(p){
var type = window.TEMPORARY;
var size = 1 * 1024 * 1024; //文件大小
$ b $ window.requestFileSystem(type,size,successCallback,errorCallback)

函数successCallback(fs){

//路径对于contact.vcf文件是:\ data \ data \''our-package-name-goes-here'\cache
fs.root.getFile('contact.vcf',{create:true} ,函数(fileEntry){

fileEntry.createWriter(function(fileWriter){
fileWriter.onwriteend = function(e){
alert('Write completed。');
//现在你可以上传这个文件到服务器

upload(fileEntry);
};

fileWriter.onerror = function(e){
alert('Write failed:'+ e.toString());
};


var vCardFile =BEGIN:VCARD \r\\\
VERSION: 3.0+\\r\\\
N:+ p.getSurname()+;+ p.getFirstName()+\r\\\
FN:+ p.getFirstName()++ p.getSurname() + \\ r \ nORG:+ p.getCompanyName()+\r\\\
TITLE:+ p.getTitle()+\r\\\
TEL; TYPE = WORK,VOICE:+ p.getWorkPhone )+\r\\\
TEL; TYPE = HOME,VOICE:+ p.getHomePhone()+\r\\\
ADR; TYPE = WORK:;;+ p.getStreet()+;+ p.getCity()+;+ p.getState()+;+ p.getPostcode()+;+ p.getCountry()+\r\\\
EMAIL; TYPE = PREF,INTERNET: + p.getEmailAddress()+\r\\\
END:VCARD \r\\\
;


fileWriter.write(vCardFile); // vCardFile是你格式化的字符串VCF格式。
},errorCallback);

},errorCallback);


$ b函数errorCallback(错误){
alert(ERROR:+ error.code)
}
}






$ b函数上传(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(发生错误: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必须是可以处理请求的URL,比如
// http://some.server.com/upload.php
ft.upload(fileURL,encodeURI(SERVER ),成功,失败,选项);
};

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

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


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: "aa@xyz.com",
        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://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);
};

After uploading, you can delete temporary vCard file using:

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

这篇关于通过手机上传服务器上的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆