从服务器读取上传的vcf文件 [英] read uploaded vcf file from server
问题描述
从先前的问题(已编辑的问题)
I have uploaded vcf file successfully on server on getting help from my previous question (the one which is edited)
现在我需要如何从服务器读取vcf文件或vcard并显示为联系电话的帮助和我的phonegap应用中的联系人姓名。
Now I need a help in how to read that vcf file or vcard from server and display as a contact number and contact name in my phonegap app.
使用的插件 cordova-plugin-file-transfer
有人可以帮忙吗?
推荐答案
使用插件cordova-plugin-file-transfer https://github.com/apache/cordova-plugin-file-transfer您可以从服务器下载文件。
Using plugin cordova-plugin-file-transfer https://github.com/apache/cordova-plugin-file-transfer you can download file from server.
要读取vcf文件,您需要 https://github.com/nilclass/vcardjs 基于JavaScript的库。您可以直接使用.js文件。
For Read vcf file, you need https://github.com/nilclass/vcardjs JavaScript based library. you can directly use .js files.
您可以按照以下示例进行操作。
You can follow below example.
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
}
这篇关于从服务器读取上传的vcf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!