Created
June 8, 2022 11:10
-
-
Save dantetesta/66e5c5cb00f96992ec198f65a6a9583b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*NAME, PHONE*/ | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <div class="row"> | |
| <div class="col-md-12"> | |
| <h1>CSV to VCard</h1> | |
| <form action="" method="post"> | |
| <div class="form-group"> | |
| <label for="csv">CSV File</label> | |
| <input type="file" name="csv" id="csv" class="form-control"> | |
| </div> | |
| <div class="form-group"> | |
| <button type="submit" class="btn btn-primary">Submit</button> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| const form = document.querySelector('form'); | |
| form.addEventListener('submit', (e) => { | |
| e.preventDefault(); | |
| const csv = document.querySelector('#csv').files[0]; | |
| const reader = new FileReader(); | |
| reader.readAsText(csv); | |
| reader.onload = (e) => { | |
| const data = e.target.result; | |
| const rows = data.split(' | |
| '); | |
| const vcards = []; | |
| rows.forEach((row) => { | |
| const columns = row.split(','); | |
| const name = columns[0]; | |
| const phone = columns[1]; | |
| const vcard = `BEGIN:VCARD | |
| VERSION:3.0 | |
| PRODID:-//Apple Inc.//iOS 13.6//EN | |
| N:;${name};;; | |
| FN:${name} | |
| TEL;type=CELL;type=VOICE;type=pref:+55${phone} | |
| REV:2020-07-27T23:39:58Z | |
| END:VCARD`; | |
| vcards.push(vcard); | |
| }); | |
| const blob = new Blob(vcards, {type: 'text/vcard'}); | |
| const url = URL.createObjectURL(blob); | |
| const link = document.createElement('a'); | |
| link.href = url; | |
| link.download = 'vcards.vcf'; | |
| link.click(); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment