从服务器读取上传的vcf文件

时间:2016-12-21 11:32:09

标签: android cordova cordova-plugins

我已经在服务器上成功上传了vcf文件,以获取我之前question(已编辑的那个)的帮助

现在我需要一个帮助,如何从服务器读取该vcf文件或vcard,并在我的phonegap应用程序中显示为联系人号码和联系人姓名。

插件使用了cordova-plugin-file-transfer

有人可以为此提供帮助吗?

1 个答案:

答案 0 :(得分:2)

使用插件cordova-plugin-file-transfer https://github.com/apache/cordova-plugin-file-transfer,您可以从服务器下载文件。

对于Read vcf文件,您需要https://github.com/nilclass/vcardjs基于JavaScript的库。你可以直接使用.js文件。

您可以按照以下示例进行操作。

 window.requestFileSystem(window.TEMPORARY, 1 * 1024 * 1024, function (fs) {

        console.log('file system open: ' + fs.name);
        var fileName = "temp.vcf";
        var dirEntry = fs.root;
        dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {

           download(fileEntry,"server-path-to-file.vcf");

        }, onErrorCreateFile);

    }, onErrorLoadFs);




function download(fileEntry, uri) {

    var fileTransfer = new FileTransfer();
    var fileURL = fileEntry.toURL();

    fileTransfer.download(
        uri,
        fileURL,
        function (entry) {
            console.log("Successful download...");
            console.log("download complete: " + entry.toURL());
            readFile(entry);
        },
        function (error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        null, // or, pass false
        {
            //headers: {
            //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            //}
        }
    );
}


function readFile(fileEntry) {
    fileEntry.file(function (file) {

        var reader = new FileReader();

        reader.onloadend = function () {

            console.log("Successful file read: " + reader.result);
            reader.parseVCard(reader.result);

        };

        reader.readAsText(file);

    }, onErrorReadFile);
}

function parseVCard(vCarddata){
VCF.parse(vCarddata, function(vcard) {
  // this function is called with a VCard instance.
  // If the input contains more than one vCard, it is called multiple times.
  console.log("Formatted name", vcard.fn);
  console.log("Names", JSON.stringify(vcard.n));
});
//Fore more help:https://github.com/nilclass/vcardjs
}