How to generate vCard dynamically using ASP.NET?
Are you looking the article on topic How to generate vCard dynamically using ASP.NET? Hereunder I will explain vCard, vCard is nothing but contact card or you can say it is a standard electronic business card. Here in this article we are going to learn how to generate a vCard dynamically using ASP.NET. Once you create the vCard it will be easy for you to transfer or send the card as an attachment.
vCard is an electronic business card which is used to transfer the contact information across the electronic machines.
Nowadays most of the people uses smart phone so vCad is getting more popularity.
In this example we will create a very simple vCard on click of a button. Normally vCard extension will be .vcf, and here in this program also we will be generating the same .vcf file. Vcf file can be opened using outlook or using Windows contact.Implementation of VCard
First we will make the interface for the vCard where user enters the data. Regular vCard require First name, Last Name, Company, Job Title, Address, City, Country, Image etc. You can see these details in below screen print.
On click of the button we will generate the vCard by taking the information user has already filled in the form.
Below screen print shows the values we are going to use in vCard Coding and code explanation
We will create properties for the values we are going to use and then get the value from the user to these variables.
Below is the complete code we will be using for this purpose. Explanation of the code will be given below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace WebApplicationPractice
{
public partial class VCard : System.Web.UI.Page
{
public string FName { get; set; }
public string LName { get; set; }
public string Company { get; set; }
public string JobTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public byte[] Image { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnVCard_Click(object sender, EventArgs e)
{
GeneratevCard();
}
public string BuildvCard()
{
var strvCardBuilder = new StringBuilder();
strvCardBuilder.AppendLine("BEGIN:VCARD");
strvCardBuilder.AppendLine("VERSION:2.1");
strvCardBuilder.AppendLine("N:" + LName + ";" + FName);
strvCardBuilder.AppendLine("FN:" + FName + " " + LName);
strvCardBuilder.Append("ADR;HOME;PREF:;;");
strvCardBuilder.Append(Address + ";");
strvCardBuilder.Append(City + ";;");
strvCardBuilder.AppendLine(Country);
strvCardBuilder.AppendLine("ORG:" + Company);
strvCardBuilder.AppendLine("TITLE:" + JobTitle);
strvCardBuilder.AppendLine("TEL;HOME;VOICE:" + Phone);
strvCardBuilder.AppendLine("TEL;CELL;VOICE:" + Mobile);
strvCardBuilder.AppendLine("EMAIL;PREF;INTERNET:" + Email);
strvCardBuilder.AppendLine("PHOTO;ENCODING=BASE64;TYPE=JPEG:");
strvCardBuilder.AppendLine(Convert.ToBase64String(Image));
strvCardBuilder.AppendLine(string.Empty);
strvCardBuilder.AppendLine(string.Empty);
strvCardBuilder.AppendLine(string.Empty);
strvCardBuilder.AppendLine("END:VCARD");
return strvCardBuilder.ToString();
}
public void GeneratevCard()
{
var vcMyCard = new VCard
{
FName = txtFName.Text,
LName = txtLName.Text,
Company = txtCompany.Text,
JobTitle = txtJobTitle.Text,
Address = txtAddress.Text,
City = txtCity.Text,
Country = txtCountry.Text,
Phone = txtPhone.Text,
Mobile = txtMobile.Text,
Email = txtEmail.Text,
};
vcMyCard.Image = File.ReadAllBytes(txtImage.Text);
using (var vCardFile = File.OpenWrite("D:\\vCard\\MyVCard.vcf"))
using (var swWriter = new StreamWriter(vCardFile))
{
swWriter.Write(vcMyCard.BuildvCard());
}
}
}
}
GeneratevCard() function gets the required values from the user and call BuildvCard() function to get the actual vCard data.
At the end we write the vCard to the path we specified using StreamWriter.
vCard has been created and saved in the folder.
Finally our vCard look like below, you can see the image and all the information we have filled in the web page,
In this example I have used a textbox to get the image path which is not right. In actual scenario we will use FileUpload control to upload the image of the user and show that in the vCard. To reduce the length to concentrate more on generating vCard I have avoided using FileUpload control.
Also you will find that we are getting the image as byte Array and then we are using Base64 encoding get the image back to original.
In this example vCard will be saved in a folder but you can extend this by sending an email or by sending SMS to the concerned parties.
Thank you,I have one more requirement Finally hosting to server I am opening to another system or mobile click on button directly download that vcf file OR ask to saving option ,
please help me