如何将vcard写入文件

时间:2017-07-27 05:09:07

标签: node.js npm vcard

我尝试通过API调用创建vcard。以下代码显示,

var vcard = require('vcard-generator');
const vcardContent = vcard({
        name: {
            givenName: 'test',
            middleName: '',
            prefix: '',
            suffix: '',
        },
        formattedNames: [{
            text: '',
        }],
        nicknames: [{
            text: '',
        }],
        works: [{
            organization: 'AMT'
        }],
        emails: [{
            type: 'work',
            text: 'test@test.com',
        }],
        phones: [{
            type: 'sms',
            text: '+14035551515',
        }, {
            uri: 'sms://+14035551515',
        }],
        urls: [{
            type: 'test1',
            uri: 'https://test1.com',
        }, {
            type: 'test2',
            uri: 'https://test2.com',
        }],
    });

    console.log(vcardContent)

如何将上述数据写为test.vcf文件或任何其他可用于处理vcard生成的npm。而且我还需要通过扫描QR码来获取生成的vcard。

1 个答案:

答案 0 :(得分:0)

Node.js fs包允许您写入文件系统。

写入文件系统

var fs = require('fs');
var vcard = require('vcard-generator');
const vcardContent = vcard(
  {
    name: {
      givenName: 'test',
      middleName: '',
      prefix: '',
      suffix: '',
    },
    formattedNames: [{
      text: '',
    }],
    nicknames: [{
      text: '',
    }],
    works: [{
      organization: 'AMT'
    }],
    emails: [{
      type: 'work',
      text: 'test@test.com',
    }],
    phones: [
      {
        type: 'sms',
        text: '+14035551515',
      }, 
      {
        uri: 'sms://+14035551515',
      }
    ],
    urls: [{
      type: 'test1',
      uri: 'https://test1.com',
    }, {
      type: 'test2',
      uri: 'https://test2.com',
    }],
});
fs.writeFile("/path/to/save/to", vardContent, function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("Your vCard was saved!");
});