Skip to content

Instantly share code, notes, and snippets.

@jasongabler
Created October 14, 2014 18:12
Show Gist options
  • Save jasongabler/94c093d154c46c01dbf9 to your computer and use it in GitHub Desktop.
Save jasongabler/94c093d154c46c01dbf9 to your computer and use it in GitHub Desktop.
Grabs vCard data from an HTML container "#vcard" and turns it into an downloading octect-stream
(function( $ ) {
$(function() {
$('.vcard-download').click(function() {
// Get the vcard data stored within the same result container
var vcard = $('#vcard');
// Generate a filename that is the person's fullname with all whitespace converted to dashes.
var filename = vcard.data('displayname').replace(/\s+/, '-') + '.vcf';
// Initiate the download of the data as a file
download(vcard.html(), filename, 'text/vcard');
});
});
// Convert data into a downloadable file stream
var download = function(content, filename, contentType){
if (!contentType) {
contentType = 'application/octet-stream';
}
var a = document.createElement('a');
var blob = new Blob([content], {'type':contentType});
a.href = window.URL.createObjectURL(blob);
a.download = filename;
a.click();
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment