In this article I will explain with an example, how to create and download vCard (.VCF) file in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an Image and some Label controls and a Button.
The Button has been assigned an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td rowspan="10" valign="top">
            <asp:Image ID="imgPhoto" runat="server" ImageUrl="~/Images/Mudassar.png" />
        </td>
    </tr>
    <tr>
        <td style="width: 100px">Name:</td>
        <td><asp:Label ID="lblName" runat="server" Text="Mudassar Khan" /></td>
    </tr>
    <tr>
        <td style="width: 100px">Company Name:</td>
        <td><asp:Label ID="lblOrganization" runat="server" Text="ASPSnippets Private Limited" /></td>
    </tr>
    <tr>
        <td style="width: 100px">Title (Position):</td>
        <td><asp:Label ID="lblTitle" runat="server" Text="Director" /></td>
    </tr>
    <tr>
        <td>Mobile Number:</td>
        <td><asp:Label ID="lblMobileNumber" runat="server" Text="9800000000" /></td>
    </tr>
    <tr>
        <td>Home Number:</td>
        <td><asp:Label ID="lblHomeNumber" runat="server" Text="6300000000" /></td>
    </tr>
    <tr>
        <td>Work Number:</td>
        <td><asp:Label ID="lblWorkNumber" runat="server" Text="7800000000" /></td>
    </tr>
    <tr>
        <td>Email Address:</td>
        <td><asp:Label ID="lblEmailAddress" runat="server" Text="mudassar.khan@aspsnippets.com" /></td>
    </tr>
    <tr>
        <td>Website:</td>
        <td><asp:Label ID="lblWebsite" runat="server" Text="www.mudassarkhan.com" /></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="txtCreate" runat="server" Text="Create VCF" OnClick="txtCreate_Click" /></td>
    </tr>
</table>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Text;
 
VB.Net
Imports System.IO
Imports System.Text
 
 
Creating and downloading vCard (.VCF) file in ASP.Net
When the Create VCF Button is clicked, the following event handler is executed.
Inside this event handler, first a StringBuilder class object is created. Then the information for creating the vCard (.VCF) is read from the Label controls and appended to the StringBuilder class object using Append function.
For the contact Photo, Image file is read from the Folder path and converted to BYTE Array and then converted to BASE64 string and finally, appended to the StringBuilder object.
Once the vCard details such as Name, Organization, Phone Numbers, Email Addresses, Websites and Photo of the contact is filled, the file is downloaded using the Response class.
Note: The ContentType of Response class is set as text/vcard which informs the Browser that the file type is a vCard.
 
C#
protected void txtCreate_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("BEGIN:VCARD\r\nVERSION:2.1");
    sb.Append(Environment.NewLine);
 
    sb.Append("N:");
    sb.Append(lblName.Text.Split(' ')[0]);
    sb.Append(";");
    sb.Append(lblName.Text.Split(' ')[1]);
    sb.Append(";");
    sb.Append(Environment.NewLine);
 
    sb.Append("FN:");
    sb.Append(lblName.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TEL;CELL:");
    sb.Append(lblMobileNumber.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TEL;HOME:");
    sb.Append(lblHomeNumber.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TEL;WORK:");
    sb.Append(lblWorkNumber.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("EMAIL;WORK:");
    sb.Append(lblEmailAddress.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("ORG:");
    sb.Append(lblOrganization.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TITLE:");
    sb.Append(lblTitle.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("URL:");
    sb.Append(lblWebsite.Text);
    sb.Append(Environment.NewLine);
 
    byte[] bytes = File.ReadAllBytes(Server.MapPath("~/Images/Mudassar.png"));
    string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
    sb.Append("PHOTO;ENCODING=BASE64;JPEG:");
    sb.Append(base64);
    sb.Append(Environment.NewLine);
 
    sb.Append("END:VCARD");
 
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Mudassar.vcf");
    Response.Charset = "";
    Response.ContentType = "text/vcard";
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub txtCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("BEGIN:VCARD" & vbCrLf & "VERSION:2.1")
    sb.Append(Environment.NewLine)
 
    sb.Append("N:")
    sb.Append(lblName.Text.Split(" "c)(0))
    sb.Append(";")
    sb.Append(lblName.Text.Split(" "c)(1))
    sb.Append(";")
    sb.Append(Environment.NewLine)
 
    sb.Append("FN:")
    sb.Append(lblName.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TEL;CELL:")
    sb.Append(lblMobileNumber.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TEL;HOME:")
    sb.Append(lblHomeNumber.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TEL;WORK:")
    sb.Append(lblWorkNumber.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("EMAIL;WORK:")
    sb.Append(lblEmailAddress.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("ORG:")
    sb.Append(lblOrganization.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TITLE:")
    sb.Append(lblTitle.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("URL:")
    sb.Append(lblWebsite.Text)
    sb.Append(Environment.NewLine)
 
    Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/Images/Mudassar.png"))
    Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
    sb.Append("PHOTO;ENCODING=BASE64;JPEG:")
    sb.Append(base64)
    sb.Append(Environment.NewLine)
 
    sb.Append("END:VCARD")
 
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=Mudassar.vcf")
    Response.Charset = ""
    Response.ContentType = "text/vcard"
    Response.Output.Write(sb.ToString())
    Response.Flush()
   Response.End()
End Sub
 
 
Screenshot
Create and download vCard (.VCF) file in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads