最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to create vCard using javascript? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Aug 22, 2016 at 6:01 Andath 22.7k45 gold badges122 silver badges140 bronze badges asked Aug 20, 2016 at 8:59 Linh DuongLinh Duong 1691 gold badge1 silver badge6 bronze badges 4
  • 5 Have you read the vCards-js/README.md – Yosvel Quintero Commented Aug 20, 2016 at 9:03
  • Sure, I read it. But when i clone it to local. No example for me, i tried some method, but it not work! – Linh Duong Commented Aug 22, 2016 at 1:18
  • Thanks for your help! This extension use in nodejs. I found simple function to create vCard. – Linh Duong Commented Aug 24, 2016 at 7:31
  • 1 ehrm... pretty much the first thing in the README says below is a basic example. And there actually is an example! What would be the problem sir? – giorgio Commented Aug 24, 2016 at 9:31
Add a comment  | 

3 Answers 3

Reset to default 9

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());
});
var vCard = (function () {
    var start = "BEGIN:VCARD\nVERSION:3.0";
    var end = "END:VCARD";
    var data = "";

    var init = function() {
        data = "";
    };

    var name = function (surname, lastname) {
        data += "N:" + lastname + ';' + surname;
        data += "\n";
    };

    var cell = function (cell) {
        data += "TEL;TYPE=CELL:" + cell;
        data += "\n";
    };

    var work = function (work) {
        data += "TEL;TYPE=WORK,VOICE:" + work;
        data += "\n";
     };

    var other = function (work) {
        data += "TEL;TYPE=OTHER:" + work;
        data += "\n";
    };

    var email = function (email) {
        data += "EMAIL;TYPE=PREF,INTERNET:" + email;
        data += "\n";
    };

    var get = function () {
        return start + '\n' + data + end;
    };

    return {
        init:init,
        name:name,
        cell:cell,
        work:work,
        other:other,
        email:email,
        get:get
    }
});

One of the best libraries for creating vCards is ez-vcard, it is written in Java, but I created a NodeJS / TS wrapper, so now you can use it from server-side JavaScript.

npm; GitHub

npm i ez-vcard
import path from 'path';
import ezVcard, {Config, VcardData} from '../src/index';

(async () => {
  const data: VcardData = {
    name: {
      first: 'Maxim',
      last: 'Mazurok',
      full: 'Maxim Mazurok',
    },
    job: {
      title: 'Software Engineer',
      org: 'WiseTech Global',
    },
    online: {
      links: [
        'https://maxim.mazurok.com',
        'https://www.linkedin.com/in/maximmazurok',
      ],
      email: '[email protected]',
    },
    geo: {
      timezone: 'Australia/NSW',
      coordinates: {
        lat: -33.9242491,
        lon: 151.1918779,
      },
      addresses: [
        {
          street: '28 Ebsworth Street',
          city: 'Sydney',
          region: 'NSW',
          postCode: '2017',
          country: 'Australia',
          types: ['HOME'],
        },
      ],
    },
    phoneNumbers: [
      {
        number: '+61402282326',
        type: 'CELL',
      },
      {
        number: '+380984877707',
        type: 'CELL',
      },
    ],
    misc: {
      note: 'Professional web developer',
      birthday: {
        year: 1997,
        month: 'FEBRUARY',
        day: 14,
      },
      uid: '9aff8498-14c4-43ab-9ac6-40c2953c33ff',
      photo: {
        path: path.join(__dirname, 'contact_picture.jpg'),
        type: 'JPEG',
      },
      vcardUrl: 'https://maxim.mazurok.com/vcard.vcf',
      setRevision: true,
    },
  };

  const config: Config = {
    version: 'V2_1', // "V2_1" | "V3_0" | "V4_0"
    prodId: false, // remove `X-PRODID:ez-vcard 0.10.6`
  };

  const vcard = await ezVcard(data, config);

  console.log(vcard);
})();

Also, see readme and example

发布评论

评论列表(0)

  1. 暂无评论