如何使用 javascript 创建 vCard?

新手上路,请多包涵

我找到了一个 扩展 来解决我的问题。但是当我将它克隆到本地时,它没有任何示例。

我对如何使用它感到困惑。我尝试了一些方法,但它对我不起作用。请告诉我如何使用它或任何扩展来解决我的问题?

原文由 Linh Duong 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 714
1 个回答

正如您在评论中所写:是的:vCards JS 使用 NodeJS。

根据 vCards-js/README.md

安装:

 npm install vcards-js --save

用法:

如何创建基本 vCard 以及如何将其保存到文件或从控制台查看其内容的简单示例:

 const vCard = require('vcards-js');

//create a new vCard
vCard = vCard();

//set properties
vCard.firstName = 'Eric';
vCard.middleName = 'J';
vCard.lastName = 'Nesser';
vCard.organization = 'ACME Corporation';
vCard.photo.attachFromUrl('https://avatars2.githubusercontent.com/u/5659221?v=3&s=460', 'JPEG');
vCard.workPhone = '312-555-1212';
vCard.birthday = new Date('01-01-1985');
vCard.title = 'Software Developer';
vCard.url = 'https://github.com/enesser';
vCard.note = 'Notes on Eric';

//save to file
vCard.saveToFile('./eric-nesser.vcf');

//get as formatted string
console.log(vCard.getFormattedString());

您也可以在您的网站上使用 vCards JS。下面是如何让它在 Express 4 上运行的示例:

 const express = require('express');
const router = express.Router();

module.exports = function(app) {
  app.use('/', router);
};

router.get('/', function(req, res, next) {

  const vCard = require('vcards-js');

  //create a new vCard
  vCard = vCard();

  //set properties
  vCard.firstName = 'Eric';
  vCard.middleName = 'J';
  vCard.lastName = 'Nesser';
  vCard.organization = 'ACME Corporation';

  //set content-type and disposition including desired filename
  res.set('Content-Type', 'text/vcard; name="enesser.vcf"');
  res.set('Content-Disposition', 'inline; filename="enesser.vcf"');

  //send the response
  res.send(vCard.getFormattedString());
});

原文由 Yosvel Quintero 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏