enesser / vCards-js

Create vCards to import contacts into Outlook, iOS, Mac OS, and Android devices from your website or application.
MIT License
430 stars 158 forks source link

Can i use in browser with frontend only. #54

Closed jitendra1607 closed 3 years ago

jitendra1607 commented 4 years ago

Can i use in browser with frontend only.

NiklasAkita commented 4 years ago

Yes, for now at least, i have luck using it on a website. But you can't use the built in download functionality since it is based on a backend library.

Toumash commented 3 years ago

@jitendra1607 @NiklasAkita do any of you guys have a working demo?

jitendra1607 commented 3 years ago

@jitendra1607 @NiklasAkita do any of you guys have a working demo? below is my solution.

 var vcf = `BEGIN:VCARD\nVERSION:3.0\nFN;CHARSET=UTF-8:${firstname} ${middlename} ${lastname}\nN;CHARSET=UTF-8:${lastname};${firstname};${middlename};;\nEMAIL;CHARSET=UTF-8;type=HOME,INTERNET:${email}\nTEL;TYPE=HOME,VOICE:${mobile_number}\nTEL;TYPE=WORK,VOICE:${mobile_number2}\nLABEL;CHARSET=UTF-8;TYPE=WORK:${address}\nADR;CHARSET=UTF-8;TYPE=WORK:;;;;;;\nROLE;CHARSET=UTF-8:${dt.d}\nORG;CHARSET=UTF-8:${dt.o}\nURL;type=WORK;CHARSET=UTF-8:${dt.w}\nREV:2020-08-31T03:41:09.870Z\nEND:VCARD`;

                  var imgqr = document.getElementById('qr');

                  imgqr.src = 'https://chart.googleapis.com/chart?cht=qr&chs=340x340&chl=' + vcf.replace(/\n/g,'%0A');
Imperio8 commented 3 years ago

This is my solution to download it as a file:

import vCardsJS from 'vcards-js';

const download = (vCardString, fileName) => {
    let fileURL = window.URL.createObjectURL(new Blob([vCardString]));
    let fileLink = document.createElement('a');
    fileLink.href = fileURL;
    fileLink.setAttribute('download', `${fileName}.vcf`);
    document.body.appendChild(fileLink);
    fileLink.click();
};

const generateVCard= (contact) => {
        //Map the data here accordingly
    vCard.firstName = contact.name;

    //Add Other data as listed on documentation

    //Stringify it
    const formattedString = vCard.getFormattedString()
    download(formattedString , `filename`);
}

This should work in any js file. I hope this helps :)

Toumash commented 3 years ago

This issue can be closed :)