Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create vCard using javascript?

Tags:

javascript

I found an extension to resolve my problem. But when i clone it to local, it doesn't have any example.

I am confused about how to use it. I try some method, but it not work for me. Please show me how to use it or any extension to solve my problem?

like image 202
Linh Duong Avatar asked Aug 20 '16 08:08

Linh Duong


1 Answers

As you wrote in the comment yes: vCards JS uses NodeJS.

According to the vCards-js/README.md:

Install:

npm install vcards-js --save

Usage:

Simple example of how to create a basic vCard and how to save it to a file, or view its contents from the console:

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());

Also you can use vCards JS on your website. Below is an example of how to get it working on 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());
});
like image 148
Yosvel Quintero Arguelles Avatar answered Oct 04 '22 04:10

Yosvel Quintero Arguelles